Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

write csv file with double quotes for particular column not working

I'm trying to write a csv file using python csv writer.

In which one of the column value is enclosed in "" [double quotes] e.g. : 'col1' 'col2' "test", when I open the file in wordpad, the word test is expected as "test" but actual result is """test"""

can someone guide for this issue.

Sample snippet of my try out:

csvReader = csv.reader(iInputFile)
writer = csv.writer(open('one_1.csv', 'wb'), delimiter=',', lineterminator='\r\n')

for row in csvReader:
     rawRow = []
     rawRow.append('31-7-2014') #Appending Date
     rawRow.append(row[0])   #Appending data
     rawRow.append('\"'+'test'+'\"') 
     writer.writerow(rawRow)
like image 930
Emma Avatar asked Jul 31 '14 10:07

Emma


People also ask

How do I fix a double quote in a CSV file?

There are 2 accepted ways of escaping double-quotes in a CSV file. One is using a 2 consecutive double-quotes to denote 1 literal double-quote in the data. The alternative is using a backslash and a single double-quote.

What can you use to handle CSV files with quoted identifiers?

You can use alternative "delimiters" like ";" or "|" but simplest might just be quoting which is supported by most (decent) CSV libraries and most decent spreadsheets.

Why does my CSV file have quotation marks?

Quotation marks are used as text qualifiers Quotation marks appear in CSV files as text qualifiers. This means, they function to wrap together text that should be kept as one value, versus what are distinct values that should be separated out.


1 Answers

try with this one

f_writ = open('one_4.csv', 'wb')
csvReader = csv.reader(iInputFile)
writer = csv.writer(f_writ, delimiter=',',
                lineterminator='\r\n',
                quotechar = "'"
                )

for row in csvReader:

    writer.writerow(['31-7-2014',row[0],'\"text\"'])

f_writ.close()

also i find very useful this link http://pymotw.com/2/csv/, there are a lot of exemples

like image 61
GiovanniPi Avatar answered Sep 28 '22 21:09

GiovanniPi