Problem with loading textfiles into dictionaries.

M

mercuryprey

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

However, it works flawlessy in another function, where I have:

def do_add(self, arg):
sitefile = file('sitelist', 'r+', 1)
act_addlist = arg.split()
sitelist[act_addlist[0]] = act_addlist[1:]
sitefile.seek(0,2)
sitefile.write(arg + "\n")
print "Written to database."

Anyone knows why it doesn't work in the first function? Help very much
appreciated.

munin
 
S

Stephen Thorne

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

maybe you would be better off doing something slightly simpler, and in
such a way that you see the input which is causing problems.

sitelist = {}
for line in file('sitelist'):
elems = line.split()
if len(elems) == 1:
raise ValueError, "Invalid line in file %r" % line
sitelist[elem[0]] = elem[1:]

:)

Stephen
 
M

M.E.Farmer

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
 
K

Kartic

(e-mail address removed) said the following on 1/30/2005 7:43 PM:
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

Hi - It looks like your code encountered a blank line when you got this
error.

You should move "if len(siteline) == 0" part right after your readline.
The way you have done it really does not help.

def do_load(self, arg):
sitefile = file('sitelist', 'r+', 1)
while True:
siteline = sitefile.readline()
if len(siteline) == 0:
break
site_rawlist = siteline.split()
sitelist[site_rawlist[0]] = site_rawlist[1:]

Thanks,
--Kartic
 
K

Kartic

Kartic said the following on 1/30/2005 8:21 PM:
(e-mail address removed) said the following on 1/30/2005 7:43 PM:
Hi - It looks like your code encountered a blank line when you got this
error.

You should move "if len(siteline) == 0" part right after your readline.
The way you have done it really does not help.

def do_load(self, arg):
sitefile = file('sitelist', 'r+', 1)
while True:
siteline = sitefile.readline()
if len(siteline) == 0:
break
site_rawlist = siteline.split()
sitelist[site_rawlist[0]] = site_rawlist[1:]


Sorry...siteline = sitefile.readline().strip()
 
M

mercuryprey

Yeah I kind of want to 'reinvent' the pickle and I am aware of that.
The problem for me is that the output that pickle dumps to a file is
too 'cryptic' as I want the ability to edit the corresponding textfile
directly and easily, so I'm going for an own way.

But yes, Kartic and you were basically right about the line length and
checking it first. Didn't really think about it, maybe I was too
tired... :) Thanks again! Hope I'm not bothering you all with my
extremely newbie questions.

munin
 
S

Steven Bethard

Kartic said:
(e-mail address removed) said the following on 1/30/2005 7:43 PM:
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
You should move "if len(siteline) == 0" part right after your readline.
The way you have done it really does not help.

Or better yet, don't use "if len(siteline) == 0" -- use "if siteline".
See this an other examples of dubious Python:

http://www.python.org/moin/DubiousPython

Specifically, see the section on Overly Verbose Conditionals.

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top