Re: Problem with loading textfiles into dictionaries.

From: M.E.Farmer (mefjr75_at_hotmail.com)
Date: 01/31/05


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



Relevant Pages

  • Re: Automatic reloading, metaclasses, and pickle
    ... inheritance. ... def PrintHi1: ... Pickle and cPickle get confused after reloading, ... dump the objects to file using protocol 2. ...
    (comp.lang.python)
  • Re: Persist a class (not an instance)
    ... classobj types, so I subclassed Pickler to override them. ... import sys, copy_reg, pickle, new, marshal, types, StringIO ... def func_constructor: ...
    (comp.lang.python)
  • a pickles pickle
    ... I'm trying to pickle a class, and while I get no errors or anything, ... almost none of the class instance gets pickled, ... The class TrainingMatrix has no embedded classes and none of its ... def AddNewWord: ...
    (comp.lang.python)
  • Re: pre-PEP: Simple Thunks
    ... def __init__(self, filename): ... 'dump' are separate methods that share data through 'self'. ... Every type of object that one would wish to pickle ...
    (comp.lang.python)
  • Re: Optimizing a text statistics function
    ... Try pickle. ... def procWrap: ... to delete the .pickle file every time you change proc() so the ... effecient the parsing loop is, because you only have to call it once. ...
    (comp.lang.python)