Re: Problem with loading textfiles into dictionaries.
From: M.E.Farmer (mefjr75_at_hotmail.com)
Date: 01/31/05
- Next message: gene.tani_at_gmail.com: "Re: a sequence question"
- Previous message: Stephen Thorne: "Re: Problem with loading textfiles into dictionaries."
- In reply to: mercuryprey_at_gmail.com: "Problem with loading textfiles into dictionaries."
- Next in thread: mercuryprey_at_gmail.com: "Re: Problem with loading textfiles into dictionaries."
- Reply: mercuryprey_at_gmail.com: "Re: Problem with loading textfiles into dictionaries."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 30 Jan 2005 17:16:07 -0800
mercuryp...@gmail.com wrote:
> Hello,
> I want to do the following:
>
> def do_load(self, arg):
> sitefile = file('sitelist', 'r+', 1)
> while True:
> siteline = sitefile.readline()
> site_rawlist = siteline.split()
> sitelist[site_rawlist[0]] = site_rawlist[1:]
> if len(siteline) == 0:
> break
>
> I want to load a textfile into a dictionaries and use the first word
on
> a line as the key for the list, then take the remaining words of the
> line and make them values of the key. This doesn't work:
>
> File "ftp.py", line 57, in do_load
> sitelist[site_rawlist[0]] = site_rawlist[1:]
> IndexError: list index out of range
Hello again Munin,
First i'll start with a spanking!
Post your code like this:(or pick your favorite starter)
Py> def do_load(self, arg):
... sitefile = file('sitelist', 'r+', 1)
... while True:
... siteline = sitefile.readline()
... site_rawlist = siteline.split()
... sitelist[site_rawlist[0]] = site_rawlist[1:]
... if len(siteline) == 0:
... break
See how much nicer that is even if the newsfeed gets mangled it comes
out ok(mostly).
If I guess right it looks like you are trying disect a line that was
empty or only had one element.
If you check for line length first you might do better.
Py> def do_load(self, arg):
... sitefile = file('sitelist', 'r+', 1)
... while True:
... if len(siteline) == 0:
... break
... siteline = sitefile.readline()
... site_rawlist = siteline.split()
... sitelist[site_rawlist[0]] = site_rawlist[1:]
Ok next thing is this smells like you really are trying to reinvent a
sort of pickle.
If you don't know search for 'python pickle module'.
examples abound but here it is anyway:
Py> import pickle
Py> # Pickle a dictionary
Py> f = open('/tmp/mydata', 'wb')
Py> f.write(pickle.dumps(yourdict)
Py> f.close()
Py> # and it is easy to get back as well
Py> f = open('tmp/mydata', rb')
Py> pdata = f.read()
Py> f.close()
Py> yourdict = pickle.load(pdata)
hth,
M.E.Farmer
- Next message: gene.tani_at_gmail.com: "Re: a sequence question"
- Previous message: Stephen Thorne: "Re: Problem with loading textfiles into dictionaries."
- In reply to: mercuryprey_at_gmail.com: "Problem with loading textfiles into dictionaries."
- Next in thread: mercuryprey_at_gmail.com: "Re: Problem with loading textfiles into dictionaries."
- Reply: mercuryprey_at_gmail.com: "Re: Problem with loading textfiles into dictionaries."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|