Reading Psalms file into a nested dictionary

My text file structure is like this (lines reduced in length to one word for simplicity). Some verses contain colons which is why I don’t split on colons. The text file is called, “Psalms.txt”

1#1|Blessed…
1#2|But…
1#3|And…
1#4|The…
2#1|Why …
2#2|The…
2#3|Let…
2#4|He…
3#1|Lord…

The leftmost number on each line is the chapter number (chapter 1, 2, 3 etc all the way up to 150)
The number after the # is the verse number (a chapter may have a few verses or it may have over 100 verses).
The text after the | is the actual verse which might be say 10 words long or might be twice that length).

Note that I thought I needed 2 different characters to separate the data on - hence the # and the |
Please let me know if this is OK.

This is what the dictionary structure should look like:-

psalms = {

1: {1: 'Blessed...',

    2: 'But...',

    3: 'And...',

    4: 'The...'},

2: {1: 'Why...',

    2: 'The...',

    3: 'Let...',

    4: 'He...'},

3: {1: 'Lord...'}

}

I have tried for 4 days to do this. I have watched YouTube vids, I have read webpages.
I have tried

chapter_number = 0
verse_number = 0
verse = ''
verse_dictionary = {verse_number: verse }
Psalms = {chapter_number : verse_dictionary }


counter =1

filename = 'Psalms.bak'
with open(filename) as f:
    for line in f:
        key,value = line.strip().split("|")
        verse_dictionary[verse_number] = verse
        print(verse_dictionary)
        Psalms[chapter_number] = verse_dictionary 
        #print(Psalms[150][6])

I have tried:-

chapter_number = 0
verse_number = 0
verse = ''
verse_dictionary = {verse_number: verse }
Psalms = {chapter_number : verse_dictionary }


counter =1

filename = 'Psalms.txt'
with open(filename) as f:
    for line in f:
        split_line = line.split("|")
        chapter_number = split_line[0].strip()
        chapter_number = int(chapter_number)
        verse_number = split_line[1].strip()
        verse = split_line[2].strip()
        print(chapter_number)
        print(verse_number)
        print(verse)
        #print(chapter_number,verse_number,verse) # print it in any order
        # data type chapter_number:int, verse_number:str, verse:str
        verse_dictionary[verse_number] = verse
        #print(verse_dictionary)
        Psalms[chapter_number] = verse_dictionary 
        #print(Psalms[150][6])

Please would someone tell me how to read in the file and put the data into a nested dictionary?

What’s the current result that you are getting? Do you have any ideas why isn’t it like you want or what might be needed to change it?

Thank you for your time. I have received help from another source ( BowlOfRed) and it now reads the data in and puts it in a nested dictionary. One of my issues was that I didn’t realise that it is possible to read the entire text file into a variable. Then it can be worked on. The help I was given along with this realisation solved the problem.

In case this might help someone in the future, here is the code.

from collections import defaultdict

psalm_info = defaultdict(dict)

with open('Psalms.txt') as f:
    psalm_text = f.read()
    
for line in f.splitlines():
    location, verse_text = line.split("|")
    chapter, verse = location.split("#") # stored as str, not as ints.  OK?
    psalm_info[chapter][verse] = verse_text

chapter = "2"
verse = "3"
print(f"Chapter {chapter} verse {verse} contains {psalm_info[chapter][verse]}")

And to explain the file structure, here is a portion of the Psalms file.

1#1|Blessed is the man that walketh not in the counsel of the ungodly, nor standeth in the way of sinners, nor sitteth in the seat of the scornful.
1#2|But his delight is in the law of the LORD; and in his law doth he meditate day and night.
1#3|And he shall be like a tree planted by the rivers of water, that bringeth forth his fruit in his season; his leaf also shall not wither; and whatsoever he doeth shall prosper.
1#4|The ungodly are not so: but are like the chaff which the wind driveth away.
1#5|Therefore the ungodly shall not stand in the judgment, nor sinners in the congregation of the righteous.
1#6|For the LORD knoweth the way of the righteous: but the way of the ungodly shall perish.
2#1|Why do the heathen rage, and the people imagine a vain thing?
2#2|The kings of the earth set themselves, and the rulers take ...

Thanks again for taking the time to respond.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.