Problem with loading textfiles into dictionaries.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mercuryprey@gmail.com

    Problem with loading textfiles into dictionaries.

    Hello,
    I want to do the following:

    def do_load(self, arg):
    sitefile = file('sitelist' , 'r+', 1)
    while True:
    siteline = sitefile.readli ne()
    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

  • Stephen Thorne

    #2
    Re: Problem with loading textfiles into dictionaries.

    On 30 Jan 2005 16:43:26 -0800, mercuryprey@gma il.com
    <mercuryprey@gm ail.com> wrote:[color=blue]
    > Hello,
    > I want to do the following:
    >
    > def do_load(self, arg):
    > sitefile = file('sitelist' , 'r+', 1)
    > while True:
    > siteline = sitefile.readli ne()
    > site_rawlist = siteline.split( )
    > sitelist[site_rawlist[0]] = site_rawlist[1:]
    > if len(siteline) == 0:
    > break[/color]

    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

    Comment

    • M.E.Farmer

      #3
      Re: Problem with loading textfiles into dictionaries.

      mercuryp...@gma il.com wrote:[color=blue]
      > Hello,
      > I want to do the following:
      >
      > def do_load(self, arg):
      > sitefile = file('sitelist' , 'r+', 1)
      > while True:
      > siteline = sitefile.readli ne()
      > 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[/color]
      on[color=blue]
      > 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[/color]

      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.readli ne()
      .... 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.readli ne()
      .... 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(pda ta)
      hth,
      M.E.Farmer

      Comment

      • Kartic

        #4
        Re: Problem with loading textfiles into dictionaries.

        mercuryprey@gma il.com said the following on 1/30/2005 7:43 PM:[color=blue]
        > Hello,
        > I want to do the following:
        >
        > def do_load(self, arg):
        > sitefile = file('sitelist' , 'r+', 1)
        > while True:
        > siteline = sitefile.readli ne()
        > 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[/color]

        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.readli ne()
        if len(siteline) == 0:
        break
        site_rawlist = siteline.split( )
        sitelist[site_rawlist[0]] = site_rawlist[1:]

        Thanks,
        --Kartic

        Comment

        • Kartic

          #5
          Re: Problem with loading textfiles into dictionaries.

          Kartic said the following on 1/30/2005 8:21 PM:[color=blue]
          > mercuryprey@gma il.com said the following on 1/30/2005 7:43 PM:[/color]
          [color=blue]
          > 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.readli ne()
          > if len(siteline) == 0:
          > break
          > site_rawlist = siteline.split( )
          > sitelist[site_rawlist[0]] = site_rawlist[1:][/color]


          Sorry...sitelin e = sitefile.readli ne().strip()

          Comment

          • mercuryprey@gmail.com

            #6
            Re: Problem with loading textfiles into dictionaries.

            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

            Comment

            • Steven Bethard

              #7
              Re: Problem with loading textfiles into dictionaries.

              Kartic wrote:[color=blue]
              > mercuryprey@gma il.com said the following on 1/30/2005 7:43 PM:
              >[color=green]
              >> Hello,
              >> I want to do the following:
              >>
              >> def do_load(self, arg):
              >> sitefile = file('sitelist' , 'r+', 1)
              >> while True:
              >> siteline = sitefile.readli ne()
              >> site_rawlist = siteline.split( )
              >> sitelist[site_rawlist[0]] = site_rawlist[1:]
              >> if len(siteline) == 0:
              >> break
              >>[/color]
              > You should move "if len(siteline) == 0" part right after your readline.
              > The way you have done it really does not help.[/color]

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

              The official home of the Python Programming Language


              Specifically, see the section on Overly Verbose Conditionals.

              Steve

              Comment

              Working...