Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: writing a file:newbie question

1 view
Skip to first unread message

Gabriel Genellina

unread,
Feb 19, 2007, 7:30:11 PM2/19/07
to pytho...@python.org
En Mon, 19 Feb 2007 08:02:29 -0300, kavitha thankaian
<kavith...@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

0 new messages