0

So I have this file that contains 2 words each line. It looks like this.

[/lang:F    </lang:foreign>
[lipmack]   [lipsmack]
[Fang:foreign]  <lang:foreign>

the first word is incorrectly formatted and the second one is correctly formatted. I am trying to put them in a dictionary. Below is my code.

textFile = open("hinditxt1.txt", "r+")
textFile = textFile.readlines()
flist = []
for word in textFile:
    flist.append(word.split())

fdict = dict()    
for num in range(len(flist)):
    fdict[flist[num][0]] = flist[num][1]

First I split it then I try to put them in a dictionary. But for some reason I get "IndexError: list index out of range" when trying to put them in a dictionary. What can i do to fix it? Thanks!

2 Answers 2

1

It is better in python to iterate over the items of a list rather than a new range of indicies. My guess is that the IndexError is coming from a line in the input file that is blank or does not contain any spaces.

with open("input.txt", 'r') as f:
    flist = [line.split() for line in f]

fdict = {}
for k, v in flist:
    fdict[k] = v

print(fdict)

The code above avoids needing to access elements of the list using an index by simply iterating over the items of the list itself. We can further simplify this by using a dict comprehension:

with open("input.txt", 'r') as f:
    flist = [line.split() for line in f]

fdict = {k: v for k, v in flist}
print(fdict)
Sign up to request clarification or add additional context in comments.

Comments

0

With dictionaries it is typical to use the .update() method to add new key-value pairs. It would look more like:

for num in range(len(flist)):
    fdict.update({flist[num][0] : flist[num][1]})

A full example without file reading would look like:

in_words = ["[/lang:F    </lang:foreign>",
"[lipmack]   [lipsmack]",
"[Fang:foreign]  <lang:foreign>"]

flist = []
for word in in_words:
    flist.append(word.split())

fdict = dict()
for num in range(len(flist)):
    fdict.update({flist[num][0]: flist[num][1]})

print(fdict)

Yielding:

{'[lipmack]': '[lipsmack]', '[Fang:foreign]': '<lang:foreign>', '[/lang:F': '</lang:foreign>'}

Although your output may vary, since dictionaries do not maintain order.

As @Alex points out, the IndexError is likely from your data having improperly formatted data (i.e. a line with only 1 or 0 items on it). I suspect the most likely cause of this would be a \n at the end of your file that is causing the last line(s) to be blank.

7 Comments

update is not the only way to add a new key value pairs to the dict. This also does not address the IndexError exception that is being raise from the list object.
@Alex I could not reproduce the IndexError so I do not know how I would go about fixing something that is not broken for me. It would be an awesome service to OP if you include an answer with your favorite method of updating dictionaries, if it is not .update(). Perhaps you can reproduce the IndexError issue as well!
The index error is likely coming from something that is in the input text file that is not formatted with 2 entries (one not shown in the example)
@Reedinationer Thanks for this solution but the problem is my text file is not in a list and I am still getting the IndexError. My text file contains just 2 words in each like such as WordA WordB.
@Alex THat probably might be the case because I have Hindi text in it for examlp e [जानता [noise]
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.