Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dictionary from file
#1
input_file = open('dictionaryHmong.txt')
dictionary = {}
for lines in input_file:
    information = lines.split(',')
    dictionary[lines[0]] = {'Hmong': lines[1], 'English': lines[2]}
Error:
<module> dictionary[lines[0]] = {'Hmong': lines[1], 'English': lines[2]} builtins.IndexError: string index out of range
So I am trying to set up a dictionary from a text file but I have tried multiple ways of doing it and this was my last try with no luck.
The file looks like this:
1,raws li,as
2,kuv,I
3,nws,his
and so on

I need to get this to work before i start the rest of the program.

Thanks in advance!
Reply
#2
dictionary[lines[0]] = {'Hmong': lines[1], 'English': lines[2]}
I believe you want information here instead of lines.
When faced with such issues you can simply use print to see what kind of data you are dealing with and thus find mistakes immediately.
As for working with files, it is recommended to use context manager (with keyword). See an example in the docs.
Reply
#3
so something like this but I am still getting a error.
dictionary = {}
with open('dictionaryHmong.txt', 'r') as input_file:
    for lines in input_file:
        info = lines.split(',')
        dictionary[info[0]] = {'Hmong': info[1], 'English': info[2]}
print (dictionary)
Error:
dictionary[info[0]] = {'Hmong': info[1], 'English': info[2]} builtins.IndexError: list index out of range
I don't understand why it is saying it is out of range. there should be 3 values it is splitting up on each line.
Reply
#4
Why not insert a print after assigning info, to see what value it holds.
Reply
#5
I did and it does not make any sense. So back to the drawing board.
['ÿþ1\x00', '\x00r\x00a\x00w\x00s\x00 \x00l\x00i\x00', '\x00a\x00s\x00']
['\x00']

I figured it out my text file was saved as a unicode file instead of ANSI. At least when I switched it that fixed the problem with what was printing. Thanks.
Reply
#6
Well done solving the issue, thanks for reporting the solution.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  dictionary output to text file (beginner) Delg_Dankil 2 1,187 Jul-12-2023, 11:45 AM
Last Post: deanhystad
  Using dictionary to find the most sent emails from a file siliusu 6 7,588 Apr-22-2021, 06:07 PM
Last Post: siliusu
  Updating dictionary in another py file tommy_voet 1 4,893 Mar-28-2021, 07:25 PM
Last Post: buran
  Making a dictionary from a file instyabam 0 1,509 Oct-27-2020, 11:59 AM
Last Post: instyabam
  how can i create a dictionary of dictionaries from a file Astone 2 2,263 Oct-26-2020, 02:40 PM
Last Post: DeaD_EyE
  Convert all actions through functions, fill the dictionary from a file Astone 3 2,444 Oct-26-2020, 09:11 AM
Last Post: DeaD_EyE
  how to put text file in a dictionary infected400 2 3,005 Jan-06-2019, 04:43 PM
Last Post: micseydel
  Dictionary to .txt or .csv file stanthaman42 9 4,683 Aug-08-2018, 03:37 PM
Last Post: Vysero
  Dictionary + File Reading palmtrees 2 4,878 Nov-15-2016, 05:16 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020