On Sep 24, 2007, at 10:33 AM, Jeff Johnson wrote:
Ed:  You are correct.  My list has only the data and I am trying to
write it as a string to a file with a new line at the end of each
record.  Each record has the same number of fields.

        OK, I'm going to take the Dive Into Python approach, and show you  
the solution, followed by an explanation of each piece. I'm going to  
simulate the data with a list of strings representing the first 60  
numbers.

1       # Number of fields per record
2       flds = 3
3       data = ["%s" % i for i in range(60)]
4       nthFields = [data[n::flds] for n in range(flds)]
5       recTuples = zip(*nthFields)
6       recs = [",".join(tup) for tup in recTuples]
7       ouput = "\n".join(recs)
8       file("data.txt", "w").write(output)

        Line 3 creates the data; it's just the strings ["0", "1", ...,"59"].  
I used strings because the join commands used later on require string  
values.

        Line 4 is a list comprehension. The inner part, 'data[n::flds]', is  
a slice notation that means "start at element 'n', go to the end, and  
give me every 'flds' element". So in our case, this will be every 3rd  
element, so this will return the list ["0", "3", "6", "9", ...] when  
n=0; it will be ["1", "4", "7", "10", ...] when n=1, etc.. The  
comprehension is for n in range(flds), which means that n will be 0,  
then 1, then 2, resulting in three big lists. That is what the  
nthFields name represents, and it looks like:
        
[['0', '3', '6', '9', '12', '15', '18', '21', '24', '27', '30', '33',  
'36', '39', '42', '45', '48', '51', '54', '57'], ['1', '4', '7',  
'10', '13', '16', '19', '22', '25', '28', '31', '34', '37', '40',  
'43', '46', '49', '52', '55', '58'], ['2', '5', '8', '11', '14',  
'17', '20', '23', '26', '29', '32', '35', '38', '41', '44', '47',  
'50', '53', '56', '59']]
        
        Line 5 uses the zip function to merge the nth element of each list  
into a tuple. It takes as many arguments as you have lists. Out  
'nthFields' is a single list with 3 sub-lists as the elements, so we  
would need to write this as:
        
        zip(nthFields[0], nthFields[1], nthFields[2])
        
        But this doesn't scale; what if we changed 'flds' to 2 or 6? So  
instead of hardcoding the arguments, we use the *params syntax to  
expand the nthFields list into its separate elements; this way the zip 
() function receives 3 lists. The resulting value of recTuples is now:
        
[('0', '1', '2'), ('3', '4', '5'), ('6', '7', '8'), ('9', '10',  
'11'), ('12', '13', '14'), ('15', '16', '17'), ('18', '19', '20'),  
('21', '22', '23'), ('24', '25', '26'), ('27', '28', '29'), ('30',  
'31', '32'), ('33', '34', '35'), ('36', '37', '38'), ('39', '40',  
'41'), ('42', '43', '44'), ('45', '46', '47'), ('48', '49', '50'),  
('51', '52', '53'), ('54', '55', '56'), ('57', '58', '59')]

        Note that we now have the data broken up into one tuple for each  
record. Each tuple, though, contains separate fields; we want to join  
them into a single string that will be written for that record.  
That's what line 6 does: first it uses the join() method of strings  
to join the elements of each tuple into a comma-separated list, and  
then uses a list comprehension to iterate over all the tuples,  
creating a list with one string element for each record. The value of  
'recs' at this point is:
        
['0,1,2', '3,4,5', '6,7,8', '9,10,11', '12,13,14', '15,16,17',  
'18,19,20', '21,22,23', '24,25,26', '27,28,29', '30,31,32',  
'33,34,35', '36,37,38', '39,40,41', '42,43,44', '45,46,47',  
'48,49,50', '51,52,53', '54,55,56', '57,58,59']

        Now we have every record converted to a single string. We need to  
join these records with newlines; that's what line 7 does. Line 8  
writes the output to the file.
        
        There are probably several other ways to accomplish this task; I  
chose this one because it's very flexible. Try changing the value of  
'flds' to any other value, and see how the output changes.

-- Ed Leafe
-- http://leafe.com
-- http://dabodev.com



_______________________________________________
Post Messages to: Dabo-users@leafe.com
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-users
Searchable Archives: http://leafe.com/archives/search/dabo-users
This message: http://leafe.com/archives/byMID/dabo-users/[EMAIL PROTECTED]

Reply via email to