By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,235 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,235 IT Pros & Developers. It's quick & easy.

How to group data read from a file?

P: 30
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
Feb 12 '11 #1

✓ answered by bvdet

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().

Share this Question
Share on Google+
10 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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.
Feb 12 '11 #2

P: 30
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:
Expand|Select|Wrap|Line Numbers
  1. def main():
  2.  
  3.     new_rows = {}
  4.     values = []
  5.  
  6.     f=open('test.txt')
  7.     for line in f:
  8.  
  9.         values.append(line.rstrip('\n')[-1])
  10.  
  11.  
  12.         x = []         
  13.         x += values    
  14.  
  15.         for count in xrange(len(values)/4):
  16.             temp_values = (x.pop(3))+(x.pop(2))+(x.pop(1))+(x.pop(0))
  17.             new_rows['ROW'+str(count+1)] = temp_values
  18.         print new_rows        
  19.  
  20. if __name__ == '__main__':
  21.     main()
  22.  
Feb 13 '11 #3

P: 30
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
Feb 13 '11 #4

bvdet
Expert Mod 2.5K+
P: 2,851
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.
Expand|Select|Wrap|Line Numbers
  1. def main():
  2.     new_rows = []
  3.     values = []
  4.     f=open('test.txt')
  5.     f.next()
  6.     for line in f:
  7.         values.append(line.rstrip('\n')[-1])
  8.     f.close()
  9.     for count in xrange(0, len(values), 4):
  10.         new_rows.append(('ROW'+str((count/4)+1),values[count:count+4]))
  11.     print "\n".join(["%s=%s" % (item[0], "".join(item[1])) for item in new_rows])
  12.  
  13.  
  14. if __name__ == '__main__':
  15.     main()
Here's the way I was suggesting:
Expand|Select|Wrap|Line Numbers
  1. f = open("test.txt")
  2. f.next()
  3. dataList = []
  4. strList = []
  5. for i, line in enumerate(f):
  6.     strList.append(line.strip().split()[1])
  7.     if not (i+1)%4:
  8.         dataList.append("".join(strList))
  9.         strList = []
  10. f.close()
  11. output = "\n".join(["ROW%s=%s" % (i+1, dataList[i]) for i in range(len(dataList))])
  12. print output
Feb 13 '11 #5

bvdet
Expert Mod 2.5K+
P: 2,851
For a Python editor, I prefer Pythonwin over Idle. There are several other good editors out there.
Feb 13 '11 #6

P: 30
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:
Expand|Select|Wrap|Line Numbers
  1. f = open("test2.txt") 
  2. f.next() 
  3. dataList = [] 
  4. strList = [] 
  5. for i, line in enumerate(f): 
  6.     strList.append(line.strip().split()[-1]) 
  7.     if not (i+1)%128: 
  8.         dataList.append("".join(strList)) 
  9.         strList = [] 
  10. f.close() 
  11. output = "\n".join(["ROW%s=%s" % (i+1, dataList[i]) for i in range(len(dataList))]) 
  12. print output 
  13.  
and it seems to be working too. :)
Attached Files
File Type: txt test2.txt (15.6 KB, 342 views)
Feb 13 '11 #7

bvdet
Expert Mod 2.5K+
P: 2,851
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().
Feb 14 '11 #8

P: 30
i notice the code extracts until 128x4=512. there are 8 characters being truncated. total there are 520. how do i resolve this? tq
Feb 14 '11 #9

bvdet
Expert Mod 2.5K+
P: 2,851
strList will contain data. Test for this condition and add to output as needed.
Feb 14 '11 #10

P: 30
thanks for your advice. :)
Feb 14 '11 #11

Post your reply

Sign in to post your reply or Sign up for a free account.