简体   繁体   中英

How to remove double quotes in csv file with Python

I've managed to split a row of numbers separated by commas into new columns with each number in a row. However,i am not sure how to remove the double quote from the start and end of each row.

This is my code :

import csv
from csv import writer


COLUMNS = 6

with open("Winning No - Sheet1.csv", "r") as input:
    with open("output_file.csv", "w") as f:
        output = writer(f, delimiter=";")
        output.writerow(["Col {}".format(i+1) for i in xrange(COLUMNS)])
        for row in input:
            output.writerow(row.split(','))

Output that i get:

COL1 COL2 COL3 COL4 COL5 COL6 
"22    23   25   32   33   36"

p/s: Need to remove the double quotes from col1 and col6.

I edited my code into, but i still didn't get the output that i wanted :

output = writer(f, delimiter=";", quoting=csv.QUOTE_NONE, doublequote=False, escapechar=' ')

Just use the .strip() method, which you call on a string, and which takes a single argument - the string sequence you want to remove from the left and right sides of a string. Eg

s = '$$cat$$'
s.strip('$') # results in 'cat'

Your example:

import csv
from csv import writer

COLUMNS = 6

with open("Winning No - Sheet1.csv", "r") as input:
    with open("output_file.csv", "w") as f:
        output = writer(f, delimiter=";")
        output.writerow(["Col {}".format(i+1) for i in xrange(COLUMNS)])
        for row in input:
            output.writerow(row.strip('"').split(','))

This code worked for me :

output.writerow(row.replace('\"','').split(','))

Thank you so much for everyone.

use this for the output.

var str="test"
str.replace(/^"|"$/g, '')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM