writing a file:newbie question

kavitha thankaian kavithapython at yahoo.co.in
Mon Feb 26 05:06:28 EST 2007


Hi All,
   
  Thanks for your reply Gabriel ,,,
  I just had to use a backslash character to write newline in the 
proper loop.So I could solve that problem.But now my question is I would 
like to delete the comma at the end.
  say for example,,i have a file test.txt and the file has the list
   
  a,b,c,d,
   
  i would like to delete the trailing comma at the end,,,
   
  if someone knows pls write to me,,,
   
  kavitha


Gabriel Genellina <gagsl-py at yahoo.com.ar> wrote:  En Mon, 19 Feb 2007 08:02:29 -0300, kavitha thankaian 
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

-- 
http://mail.python.org/mailman/listinfo/python-list


 				
---------------------------------
 Here’s a new way to find what you're looking for - Yahoo! Answers 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070226/a4298535/attachment.html>


More information about the Python-list mailing list