Data Manipulation - Rows to Columns

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Feb 5 23:02:21 EST 2008


En Wed, 06 Feb 2008 01:52:08 -0200, Gabriel Genellina  
<gagsl-py2 at yahoo.com.ar> escribió:

> I have a text file with marked up data that I need to convert into a
> text tab separated file.

Sorry, I sent the code too early:

rows = []
row = None
for line in file1:
     line = line.strip() # remove leading and trailing whitespace
     if line.startswith('<item>'):
         if row: rows.append(row)
         j = line.index("</")
         item = line[6:j]
         row = [item]
     elif line.startswith('<color>'):
         j = line.index("</")
         color = line[7:j]
         row.append(color)
     else:
         raise ValueError, "can't understand line: %r" % line
if row: rows.append(row)

(some `row` should have been `line`)
In the second part, the right dialect name is 'excel-tab' instead of  
'excel_tab'.

-- 
Gabriel Genellina




More information about the Python-list mailing list