Code examples
Here are provided some examples of files parsing code for the most common usecases
Get translations in <surah, ayah [, word], text> format
The most common way to use a translation is to get it in csv-like format, i.e. like <surah, ayah, text> (or <surah, ayah, word, text> for word-by-word translations) format. To do so, one can easily read a JSON translation file:
import json
# Read the json translation-file
with open(path_to_json_file, "r") as translation_file:
translation = json.load(translation_file)
# Loop over surahs-ayahs-words
for surah in translation["content"]["surahs"]:
for ayah in surah["ayahs"]:
print(surah["number"], ayah["number"], ayah["text"]) # Put your code here
# For a word-by-word translation add one more loop:
#for word in ayah["words"]:
# print(surah["number"], ayah["number"], word["number"], word["text"])
<?php
// Read the json translation-file
$translation_file = file_get_contents($path_to_json_file);
$translation = json_decode($translation_file, true);
// Loop over surahs-ayahs-words
foreach ($translation['content']['surahs'] as $surah) {
foreach ($surah['ayahs'] as $ayah) {
echo $surah['number'], $ayah['number'], $ayah['text']; // Put your code here
// For a word-by-word translation add one more loop:
//foreach ($ayah['words'] as $word) {
// echo $surah['number'], $ayah['number'], $word['number'], $word['text'];
//}
}
}You can do the same, using XML formatting.
from xml.etree import ElementTree as ET
# Read the XML translation-file
translation = ET.parse(path_to_xml_file).getroot()
# Loop over surahs-ayahs-words
for surah in translation.findall("content/surahs/surah"):
for ayah in surah.findall("ayahs/ayah"):
print(surah.attrib["number"], ayah.attrib["number"], ayah.text) # Put your code here
# For a word-by-word translation add one more loop:
#for word in ayah.findall("words/word"):
# print(surah.attrib["number"], ayah.attrib["number"], word.attrib["number"], word.text)
Get meta information
Here are some examples how one can read meta-data from the files
Last updated
Was this helpful?