I’ve been trying to do this for 3 days now. I have watched YouTubes, looked up nested dictionaries on about a dozen different sites (e.g. https://discuss.python.org/t/how-to-create-a-dictionary-from-scanning-text/9676/2 but I still can’t do what must be a simple thing.
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.
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).
What I am trying to achieve is a nested dictionary that looks like:
Psalms = {1: {‘verseNum’: 1, ‘verse’: ‘Blessed…’},
1: {‘verseNum’: 2, ‘verse’: ‘But…’},
1: {‘verseNum’: 3, ‘verse’: ‘And…’},
1: {‘verseNum’: 4, ‘verse’: ‘The…’},
2: {‘verseNum’: 1, ‘verse’: ‘Why…’},
2: {‘verseNum’: 2, ‘verse’: ‘The…’},
2: {‘verseNum’: 3, ‘verse’: ‘Let…’},
2: {‘verseNum’: 4, ‘verse’: ‘He…’},
3: {‘verseNum’: 1, ‘verse’: ‘Lord…’},
…
150: {‘verseNum’: 6, ‘verse’: ‘Let…’} }
Here is my code which is able to correctly print the first chapter.
print(’\n\nSTART’)
PsalmsDict = {}
PsalmsDict[1]={}
verseDict ={}
counter = 1
filename = ‘test.txt’
with open(filename) as f:
lines = f.readlines()
while counter < 160:
for line in lines:
split_line = line.split("#")
chapNum = split_line[0].strip()
chapNum = int(chapNum)
print(‘chapNum’,chapNum) # debugging ouput statement
temp = split_line[1].strip()
#print(temp)
second = temp.split(’|’)
verseNum=second[0]
verse =second[1]
#print(str(chapNum)+’::’+verseNum+’ = '+verse)
# now put it into a nested dictionary
if chapNum == counter:
print('True') # debugging ouput statement
PsalmsDict[counter]['verseNum'] = verseNum
PsalmsDict[counter]['verse']= verse
print(PsalmsDict) # debugging ouput statement
else:
PsalmsDict[chapNum] = PsalmsDict.get(chapNum, 0) + 1
counter +=1
#print('increment counter')
print(PsalmsDict) # debugging ouput statement
Here is my output.
START
chapNum 1
True
{1: {‘verseNum’: ‘1’, ‘verse’: ‘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.’}}
chapNum 1
True
{1: {‘verseNum’: ‘2’, ‘verse’: ‘But his delight is in the law of the LORD; and in his law doth he meditate day and night.’}}
chapNum 1
True
{1: {‘verseNum’: ‘3’, ‘verse’: ‘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.’}}
chapNum 1
True
{1: {‘verseNum’: ‘4’, ‘verse’: ‘The ungodly are not so: but are like the chaff which the wind driveth away.’}}
chapNum 1
True
{1: {‘verseNum’: ‘5’, ‘verse’: ‘Therefore the ungodly shall not stand in the judgment, nor sinners in the congregation of the righteous.’}}
chapNum 1
True
{1: {‘verseNum’: ‘6’, ‘verse’: ‘For the LORD knoweth the way of the righteous: but the way of the ungodly shall perish.’}}
chapNum 2
chapNum 2
True
Traceback (most recent call last):
File “main.py”, line 66, in
PsalmsDict[counter][‘verseNum’] = verseNum
TypeError: ‘int’ object does not support item assignment
Please would someone help me to figure this out?