file to nested list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • v13tn1g
    New Member
    • Feb 2009
    • 31

    file to nested list

    I'm having alot of troubles with this...so basically i am given a file for example:

    File =

    >123
    qwerty
    >567
    tyuiyuy
    >987
    poiuyt

    and basically the output is supposed to be [[">123" , "qwerty"] , [">567" , "tyuiuy"] , ["987" , "poiuyt"]]

    i have no idea how to do that can someone help me please?.
  • musicfreak
    New Member
    • Jan 2009
    • 17

    #2
    I'm assuming you know basic file I/O?

    Basically, what you need to do is create an empty list (let's call it 'root'), open the file you want with open() (let's call this 'file'), then have a loop where you get two lines at a time using file.readline() (store these in two separate variables, 'a' and 'b' for example). Then you need to do root.append([a, b]), where [a, b] will end up being [">123" , "qwerty"] from your example. When there are no more lines to read, stop the loop, and there you have your list (root).

    Does that make any sense?

    Comment

    • v13tn1g
      New Member
      • Feb 2009
      • 31

      #3
      Originally posted by musicfreak
      I'm assuming you know basic file I/O?

      Basically, what you need to do is create an empty list (let's call it 'root'), open the file you want with open() (let's call this 'file'), then have a loop where you get two lines at a time using file.readline() (store these in two separate variables, 'a' and 'b' for example). Then you need to do root.append([a, b]), where [a, b] will end up being [">123" , "qwerty"] from your example. When there are no more lines to read, stop the loop, and there you have your list (root).

      Does that make any sense?
      yep that makes a lot of sense, and i understand fully what your trying to say except i forgot to say that the file could also be

      file =

      >123
      qwerty
      ioweio
      >567
      tyuiyuy
      >987
      poiuyt

      output = [[">123" , "qwertyiowe io"] , [">567" , "tyuiuy"] , ["987" , "poiuyt"]]

      the output for >123 is "qwertyiowe io" because for simplicity's sake i made the file look that way to look simple, so basically after "qwerty" it went to a new line because it was full. i hope that made sense....

      also is there a way to use a for loop for this question?

      Comment

      • musicfreak
        New Member
        • Jan 2009
        • 17

        #4
        Oh I see. Well in that case, you're going to need to make a temporary list for each "sub list," and if the line starts with '>', make that line the first part of the list, and vice versa. Then just append that list to the 'root'.

        You could use a for loop, although it would be easier not to. Is this for an assignment? If you have to use a for loop, then you could read the file all at once into a string and split() it by newlines, and then loop through each line (for line in lines:, or something like that).

        EDIT: Actually, now that I think about it, the way I just mentioned would probably be simpler haha.

        Comment

        • v13tn1g
          New Member
          • Feb 2009
          • 31

          #5
          is your way by using a while loop?...=S(whic h i hate)..

          Comment

          • musicfreak
            New Member
            • Jan 2009
            • 17

            #6
            Yeah, the first method I mentioned would use a while loop (it was the first thing that popped into my head since that's how I learned it), but I guess using a for loop would, in the end, be much simpler and cleaner. Go for the 'for' loop. :)

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              Let us assume that you have read the file using file object method readlines().
              Code:
              data = ['>123\n', 'qwerty\n', 'ioweio\n', '>567\n', 'tyuiyuy\n', '>987\n', 'poiuyt\n']
              
              output = []
              
              for item in data:
                  if item.startswith('>'):
                      output.append([item.strip(), ''])
                  else:
                      output[-1][1] += item.strip()
              
              print output
              Output:
              >>> [['>123', 'qwertyioweio'], ['>567', 'tyuiyuy'], ['>987', 'poiuyt']]
              >>>

              Comment

              • v13tn1g
                New Member
                • Feb 2009
                • 31

                #8
                i think im close...can you debug my code

                Code:
                import urllib
                
                def qwerty(f):
                    
                    a = urllib.urlopen(f)
                    s = ""
                    l = a.readlines()
                    y = s.join(l)
                    #p = y.replace('\r', "")
                    q = y.split('\r')
                    
                    output = [] 
                  
                    for item in q: 
                        if item.startswith('>'): 
                            output.append([item.strip(), '']) 
                        else: 
                            output[-1][1] += item.strip() 
                  
                    print output
                and the output that comes out when i test it with

                Code:
                qwerty("http://www.utsc.utoronto.ca/~szamosi/a20/assignments/a3/starter/sequences.txt")
                is it will give me the whole thing as a list within a list, it doesnt seperate the numbers within...becaus e in you "data" string the \n appears after the string whereas in mine it appears before..i think =s

                Comment

                • bvdet
                  Recognized Expert Specialist
                  • Oct 2006
                  • 2851

                  #9
                  You have made it too complicated. There is no need to join, split, or replace. String method strip() will remove all leading and training whitespace. You created an open file object, but never closed it. You should try to make your variable names more descriptive instead of using single letters. In my example, variable data is comparable to your variable l.

                  HTH

                  Comment

                  • v13tn1g
                    New Member
                    • Feb 2009
                    • 31

                    #10
                    ah ok now i see...thanks for your repliess!!

                    Comment

                    • boxfish
                      Recognized Expert Contributor
                      • Mar 2008
                      • 469

                      #11
                      Originally posted by bvdet
                      You should try to make your variable names more descriptive instead of using single letters. In my example, variable data is comparable to your variable l.
                      Sorry, bvdet, but read this. How about calling it file_lines instead?

                      Comment

                      • bvdet
                        Recognized Expert Specialist
                        • Oct 2006
                        • 2851

                        #12
                        Originally posted by boxfish
                        Sorry, bvdet, but read this. How about calling it file_lines instead?
                        That would work. I sometimes name a variable of short duration, such as an open file object (f) or loop counter (i), with a single letter. Stringing several in a row makes the code unreadable. I might do something like this:
                        Code:
                        def qwerty(fn): # fn for file name
                         
                            f = urllib.urlopen(fn) # **EDIT**
                            fList = a.readlines() # or lineList or line_list or lines
                            f.close()

                        Comment

                        • boxfish
                          Recognized Expert Contributor
                          • Mar 2008
                          • 469

                          #13
                          Yeah, all my loop variables are called i, j, k, l, m, n, o, etc. It makes my nested loops unreadable.
                          Edit:
                          Wow, urllib is a built in library I can use! I had no idea.

                          Comment

                          Working...