Access Quran in Python – 23 languages with dataset
As-Salaam-Alaikum, in this tutorial, I will show you how to get Quran data and print ayah using Python.
First of all, let me tell you one thing. In this post, I will show you just basic methods to fetch or retrieve data from Quran CSV data file. I will just guide you but the creativity is in your hand. There might be a lot of work to be done to make the project perfect for you.
Some pros and cons of the dataset that I am going to use here:
- Each row has been started with Surah number, verse number in this format: 1|1, 1|2 and so on.
At first, you might be thinking it’s not good for you to deal with it. But it can be useful for you as well. You can build a program in Python that will detect these prefix and from that, you can easily tell which surah is this or which verse is this.
Dataset link:
Download it and extract it to continue this tutorial. (I found these dataset from Kaggle)
I personally don’t like the format of the dataset but I could not find other relevant datasets for my purpose.
Once you will download and unzip this dataset you will find quran csv files in 23 different languages.
I will pick English language for my example.
If you work in other languages then you may face issues due to the encoding so I will show another example just to tell you how to get rid of that.
As you can see in the above screenshot, hello.py is my Python file.
The rest of the two files are the files that I extracted from the given zip file.
You can open the CSV files to check the columns and structure of the data.
Let’s jump to the programming part.
How to fetch Quran ayat/ayah from CSV data file in Python
Steps involved:
import csv
package.- open the CSV file.
- read the file.
- Now fetch any row from the data file.
- Join the list of items to make it a string.
Full Python program to fetch any Ayah in English language
import csv with open('D:\learn python\\English.csv', mode ='r',)as file: myQuran = csv.reader(file) row = list(myQuran) result = row[1] myAyah = ' '.join([str(a) for a in result]) print(myAyah)
Output:
Explanation:
D:\learn python\\English.csv
This is my directory/path where the data has been stored.
I used \\ because a single slash is an escape character. To learn more about this check this: escape in Python.
By using row = list(myQuran)
I am storing the data as list data type in row
variable. Each row of the excel file will be stored in this way.
result = row[1]
This will store the 2nd row of the file in result
variable. (As list index starts with zero).
Now the question is: what is the purpose of myAyah = ' '.join([str(a) for a in result])
this line of code?
The answer is: if I don’t use that I will get output like this:
['1|2|Praise be to Allah', ' the Cherisher and Sustainer of the worlds;', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
It will be printed as a list if I just print result
.
I am converting the list into a string by joining all the list items of that particular row.
Now you can fetch any ayah of the Quran just by changing the index of result = row[n]
.
Instead of n
put the index you like.
Fetch Quran ayah in different languages using Python
Now I will show you an example of a different language.
Let’s take Bengali as example:
Put the Bengali dataset CSV file in the same directory and add the file path in your program just like I did here:
import csv with open('D:\learn python\\Bangla.csv', mode ='r', encoding='utf')as file: myQuran = csv.reader(file) row = list(myQuran) result = row[0] myAyah = ' '.join([str(a) for a in result]) print(myAyah)
If you look at the code carefully, you can see that I have used encoding=’utf’.
If I don’t use it it will show an error. I will discuss about that error in my another tutorial.
In the beginning of the result you may notice the numbering: 1|2|
If you wish you can leave it as it is. Or if you wish to remove it you can trim the string easily.
In order to do that you can add
print(myAyah[4:])
We can do a lot of things with this data. Let’s try something new and let me know in the comment section below.
Leave a Reply