1.

Write a Python program to write a CSV File with custom quotes?

Answer»

CSV File with quote characters

You can write the CSV file with custom quote characters, by registering new dialects using 

csv.register_dialect( ) class of csv module,

import csv

csvData = [[‘SNO’ ,Items’], [‘l’ , ’Pen’], [‘2′, ’Book’], [‘3′, ’Pencil’]]

csv.register_dialect(‘myDialect’ , delimiter = ‘|’ ,quotechar = “” ,

quoting=csv.QUOTE_ALL)

with open(‘c:\\pyprg\ \chl3\\quote.csv’ , ‘w’) as csvFile:

writer= csv.writer(csvFile, dialect-myDialect’)

writer, write rows(csvData)

print(“writing completed”)

csvFile.close( )

When you open the “quote.csv” file in notepad, we get following output:

SI.No"Items"
1"Pen"
2"Book"
3"Pencil"

In the above program, myDialect uses pipe (|) as delimiter and quotechar as doublequote “” to write inside the file.



Discussion

No Comment Found