Home > Software engineering >  Enclosing every CSV field that having commas in between with double quotes in python
Enclosing every CSV field that having commas in between with double quotes in python

Time:12-28

I am trying to enclose the each field value in double quotes using csv module. But here the trick is we do have commas in between the values which needs to be skipped. This is the snippet i am using to enclose the values in quotes.

Data:

col1,col2
first row,This section of the badge focuses on Communications
second row,Feedback has partnered with team members, leaders, and executives receive confidential, anonymous feedback

Code snippet

import csv

with open('data.csv') as input, open('out.csv','w') as output:
    reader = csv.reader(input)
    writer = csv.writer(output, delimiter=',', quoting=csv.QUOTE_ALL)
    for line in reader:
        writer.writerow(line)

Output

"col1","col2"
"first row","This section of the badge focuses on Communications"
"second row","Feedback has partnered with team members"," leaders"," and executives receive confidential"," anonymous feedback"

Expected output

"col1","col2"
"first row","This section of the badge focuses on Communications"
"second row","Feedback has partnered with team members, leaders, and executives receive confidential, anonymous feedback"

CodePudding user response:

Since the input data is not a regular CSV file, there may be a problem to read the input file using the csv module. To solve the problem, you may directly read the lines of the file and then parse them as follows:

import csv

with open('data.csv') as fin, open('out.csv','w') as fout:
    writer = csv.writer(fout, delimiter=',', quoting=csv.QUOTE_ALL)
    for line in fin:
        writer.writerow(line.rstrip().split(',', 1))

CodePudding user response:

You can use DictReader and DictWriter with the restkey attribute:

with open('data.csv') as inp, open('out.csv', 'w') as out:
    reader = csv.DictReader(inp, restkey='colN')
    writer = csv.DictWriter(out, fieldnames=reader.fieldnames,
                            delimiter=',', quoting=csv.QUOTE_ALL)
    writer.writeheader()
    for line in reader:
        line[reader.fieldnames[-1]]  = ','.join(line.pop('colN', []))
        writer.writerow(line)

Content of out.csv:

"col1","col2"
"first row","This section of the badge focuses on Communications"
"second row","Feedback has partnered with team members leaders, and executives receive confidential, anonymous feedback"
  • Related