pythonpython-2.7csvdouble-quotes

Write CSV file with double quotes for particular column not working


I'm trying to write a CSV file using Python's csv writer.

One of the column values 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 the actual result is """test""".

Can someone please 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)

Solution

  • 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