How to group data read from a file?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maximus tee
    New Member
    • Dec 2010
    • 30

    How to group data read from a file?

    hi,

    assuming i have a file that contains below data:
    cell Bit
    0 1
    1 X
    2 1
    3 0
    4 X
    5 X
    6 X
    7 X
    8 1
    9 X
    10 0
    11 0
    12 X
    13 X
    14 1
    15 1

    how can i group every 4 bits into a row? for eg:
    ROW1=01X1
    ROW2=XXXX
    ROW3=00X1
    ROW4=11XX

    i'm using Python 2.5, Win XP.

    thanks
    maximus
    Last edited by bvdet; Feb 12 '11, 02:45 PM. Reason: Clarify title
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Here's one way:
    Open the file, assigning the object to an identifier. Initialize an empty list to contain the strings representing 4 bits and another list to contain the characters. Iterate on the file object using enumerate, skipping the first line. Append the last character of next four lines to the character list (using the modulo operator to determine when to do this), then join the character list and append it to the 4 bits list and reinitialize the character list. After the end of file is reached, close the file object and convert the list into a formatted string.

    It is simpler than it sounds. Give it a shot.

    Comment

    • maximus tee
      New Member
      • Dec 2010
      • 30

      #3
      thanks. btw, do you have any advice on a better s/w to use to debug python script. i'm only using IDLE and sometimes i get traceback error and not sure how to debug it except to that line.

      here's the code:
      Code:
      def main():
      
          new_rows = {}
          values = []
                 
          f=open('test.txt')
          for line in f:
              
              values.append(line.rstrip('\n')[-1])
              
      
              x = []         
              x += values    
      
              for count in xrange(len(values)/4):
                  temp_values = (x.pop(3))+(x.pop(2))+(x.pop(1))+(x.pop(0))
                  new_rows['ROW'+str(count+1)] = temp_values
              print new_rows        
      
      if __name__ == '__main__':
          main()

      Comment

      • maximus tee
        New Member
        • Dec 2010
        • 30

        #4
        the code seems to give wrong result from what i expect.
        result:
        {'ROW1': '1X1 ', 'ROW2': 'XXX0', 'ROW3': '0X X', 'ROW4': '1XX0'}

        but i'm expecting:
        ROW1=01X1
        ROW2=XXXX
        ROW3=00X1
        ROW4=11XX

        pls advise. tq

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          I modified your function somewhat. Variable values and x contained the same data, so I eliminated x. A dictionary is unordered, so I changed new_rows to another list. I used range with a step of 4 and slicing to get the characters. I also skipped the first line, assuming the first line contains the column labels.
          Code:
          def main():
              new_rows = []
              values = []
              f=open('test.txt')
              f.next()
              for line in f:
                  values.append(line.rstrip('\n')[-1])
              f.close()
              for count in xrange(0, len(values), 4):
                  new_rows.append(('ROW'+str((count/4)+1),values[count:count+4]))
              print "\n".join(["%s=%s" % (item[0], "".join(item[1])) for item in new_rows])
              
           
          if __name__ == '__main__':
              main()
          Here's the way I was suggesting:
          Code:
          f = open("test.txt")
          f.next()
          dataList = []
          strList = []
          for i, line in enumerate(f):
              strList.append(line.strip().split()[1])
              if not (i+1)%4:
                  dataList.append("".join(strList))
                  strList = []
          f.close()
          output = "\n".join(["ROW%s=%s" % (i+1, dataList[i]) for i in range(len(dataList))])
          print output

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            For a Python editor, I prefer Pythonwin over Idle. There are several other good editors out there.

            Comment

            • maximus tee
              New Member
              • Dec 2010
              • 30

              #7
              sorry i didnt mention that i'm using Python 2.5 and Win XP. would like to check whether next() work for Python 2.5, i thought i read it somewhere it doesnt. anyway, i tried the code, it didnt complain any error. so i guess it works on Python 2.5 too..
              i have a more complex data file as attached and i modified the code to be:
              Code:
              f = open("test2.txt") 
              f.next() 
              dataList = [] 
              strList = [] 
              for i, line in enumerate(f): 
                  strList.append(line.strip().split()[-1]) 
                  if not (i+1)%128: 
                      dataList.append("".join(strList)) 
                      strList = [] 
              f.close() 
              output = "\n".join(["ROW%s=%s" % (i+1, dataList[i]) for i in range(len(dataList))]) 
              print output
              and it seems to be working too. :)
              Attached Files

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                File object method readline() could be substituted for next(). If you don't have labels on the first line in the data file, leave out f.next().
                Last edited by bvdet; Feb 14 '11, 01:02 AM.

                Comment

                • maximus tee
                  New Member
                  • Dec 2010
                  • 30

                  #9
                  i notice the code extracts until 128x4=512. there are 8 characters being truncated. total there are 520. how do i resolve this? tq

                  Comment

                  • bvdet
                    Recognized Expert Specialist
                    • Oct 2006
                    • 2851

                    #10
                    strList will contain data. Test for this condition and add to output as needed.

                    Comment

                    • maximus tee
                      New Member
                      • Dec 2010
                      • 30

                      #11
                      thanks for your advice. :)

                      Comment

                      Working...