[Tutor] How to group data?

Steven D'Aprano steve at pearwood.info
Sun Feb 13 17:00:36 CET 2011


tee chwee liong wrote:
> hi,
>  
> i'm using Python 2.5 and Win XP.
> i want to  extract the last column of the attached text file and group 128 
characters to each row. want the result to look like:

Did you have to flood us with the ENTIRE text file? Please show some 
respect! Next time, just show us a sample of the data:

0 BC_4 SYSCLK_DN observe_only 1
1 BC_4 SYSCLK_DP observe_only 1
2 BC_4 QPI3_DRX_DN 19 input X
3 BC_4 QPI3_DRX_DP 19 input X
4 BC_4 QPI3_DRX_DN 18 input X
5 BC_4 QPI3_DRX_DP 18 input X
[...]
515 BC_1 internal X
516 BC_1 internal X
517 BC_1 internal X
518 BC_4 PWRGOOD observe_only 0
519 BC_4 VIOPWRGOOD observe_only 0


> Row1=XXXXX.............................1XXX  (total of 128 char)
> Row2=11XX...............................X1XX (total of 128 char)
[...]
>     x = []          # making a new list here, not only
>     x += values     # a new reference to the same object
> 
>     for count in xrange(0,len(values),128):
>         temp_values1 = (x.pop(9))+(x.pop(8))+(x.pop(7))+(x.pop(6))+(x.pop(5))+(x.pop(4))+(x.pop(3))+(x.pop(2))+(x.pop(1))+(x.pop(0))
>         temp_values2 = (x.pop(19))+(x.pop(18))+(x.pop(17))+(x.pop(16))+(x.pop(15))+(x.pop(14))+(x.pop(13))+(x.pop(12))+(x.pop(11))+(x.pop(10))
>         temp_values3 = (x.pop(29))+(x.pop(28))+(x.pop(27))+(x.pop(26))+(x.pop(25))+(x.pop(24))+(x.pop(23))+(x.pop(22))+(x.pop(21))+(x.pop(20))
>         temp_values4 = (x.pop(39))+(x.pop(38))+(x.pop(37))+(x.pop(36))+(x.pop(35))+(x.pop(34))+(x.pop(33))+(x.pop(32))+(x.pop(31))+(x.pop(30))
>         temp_values5 = (x.pop(49))+(x.pop(48))+(x.pop(47))+(x.pop(46))+(x.pop(45))+(x.pop(44))+(x.pop(43))+(x.pop(42))+(x.pop(41))+(x.pop(40))
>         temp_values6 = (x.pop(59))+(x.pop(58))+(x.pop(57))+(x.pop(56))+(x.pop(55))+(x.pop(54))+(x.pop(53))+(x.pop(52))+(x.pop(51))+(x.pop(50))
>         temp_values7 = (x.pop(69))+(x.pop(68))+(x.pop(67))+(x.pop(66))+(x.pop(65))+(x.pop(64))+(x.pop(63))+(x.pop(62))+(x.pop(61))+(x.pop(60))
>         temp_values8 = (x.pop(79))+(x.pop(78))+(x.pop(77))+(x.pop(76))+(x.pop(75))+(x.pop(74))+(x.pop(73))+(x.pop(72))+(x.pop(71))+(x.pop(70))
>         temp_values9 = (x.pop(89))+(x.pop(88))+(x.pop(87))+(x.pop(86))+(x.pop(85))+(x.pop(84))+(x.pop(83))+(x.pop(82))+(x.pop(81))+(x.pop(80))
>         temp_values10 = (x.pop(99))+(x.pop(98))+(x.pop(97))+(x.pop(96))+(x.pop(95))+(x.pop(94))+(x.pop(93))+(x.pop(92))+(x.pop(91))+(x.pop(90))
>         temp_values11 = (x.pop(109))+(x.pop(108))+(x.pop(107))+(x.pop(106))+(x.pop(105))+(x.pop(104))+(x.pop(103))+(x.pop(102))+(x.pop(101))+(x.pop(100))
>         temp_values12 = (x.pop(119))+(x.pop(118))+(x.pop(117))+(x.pop(116))+(x.pop(115))+(x.pop(114))+(x.pop(113))+(x.pop(112))+(x.pop(111))+(x.pop(110))
>         temp_values13 = (x.pop(127))+(x.pop(126))+(x.pop(125))+(x.pop(124))+(x.pop(123))+(x.pop(122))+(x.pop(121))+(x.pop(120)) 


Try this instead.

rows = []
counter = 1  # count row numbers
for i in range(0, len(values), 128):
     # Step through the list of values, starting at 0,
     # taking 128 items at a time.
     temp = values[i:i+128]
     # Join the values into a string.
     s = ''.join(temp)
     # And make a row with a label.
     row = "Row" + str(counter) + " = " + s
     print row
     rows.append(row)




-- 
Steven


More information about the Tutor mailing list