Problem with loading textfiles into dictionaries.

Stephen Thorne stephen.thorne at gmail.com
Sun Jan 30 20:14:21 EST 2005


On 30 Jan 2005 16:43:26 -0800, mercuryprey at gmail.com
<mercuryprey at 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

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



More information about the Python-list mailing list