[csv module] duplication of end of line character in output file generated

Kent Johnson kent3737 at yahoo.com
Tue Jan 11 08:13:27 EST 2005


simon.alexandre wrote:
> Hi all,
> 
> I use csv module included in python 2.3. I use the writer and encouter the
> following problem: in my output file (.csv) there is a duplication of the
> end of line character, so when I open the csv file in Ms-Excel a blank line
> is inserted between each data line.

 From the docs for csv.writer():
writer(  	csvfile[, dialect='excel'[, fmtparam]])
     ...If csvfile is a file object, it must be opened with the 'b' flag on platforms where that 
makes a difference.

Windows is a "platform where that makes a difference." So try
   self.writer = csv.writer(file("Test.csv", "wb"))

Kent

> 
> OS: W2k
> 
> Someone has an idea ?
> 
> thanks in advance
> 
> the source code is the following:
> 
> -------------------------------------------------->
> import csv
> 
> class CsvDumper:
> 
>    def __init__(self):
>        self.object =
> [['la','mb','mc','md'],['ma','mb','mc','md'],['ma','mb','mc','md']]
>        self.writer = csv.writer(file("Test.csv", "w"))
> 
>    def DumpCsvFile(self):
>       for row in self.object:
>       self.writer.writerow(row)
> 
> c = CsvDumper()
> c.DumpCsvFile()
> 
> 



More information about the Python-list mailing list