writing a file:newbie question

Gabriel Genellina gagsl-py at yahoo.com.ar
Mon Feb 19 19:30:11 EST 2007


En Mon, 19 Feb 2007 08:02:29 -0300, kavitha thankaian  
<kavithapython at yahoo.co.in> escribió:

> Hi,
>  i have a file test.txt and it contains a list of strings say,,,
>  "a","b","c","d","a1","b1","c1","d1","a2","b2","c2","d2",
>  i would like to write the file as
>  "a","b","c","d"
>   "a1","b1","c1","d1
>   "a2","b2","c2","d2"
>  and would like to delete the comma at the end.

Not enough info...
Does the input file contain only one line, or many lines?
Always exactly 12 items? Including a trailing , ?

The following may work for 12 items. Use the csv module to read the file:

import csv
reader = csv.reader(open("test.txt", "r"))
writer = csv.writer(open("output.txt", "w"))
for row in reader:
     writer.writerow(row[:4])
     writer.writerow(row[4:8])
     writer.writerow(row[8:])

-- 
Gabriel Genellina




More information about the Python-list mailing list