简体   繁体   中英

how to avoid quoting in csv while deleting symbols?

trying to delete symbol , from my csv file but I am getting quotes and also nothing deletes at all

import csv
import string

input_file = open('/Users/gfidarov/Desktop/crosscheckmmm/Sheet1', 'r')
output_file = open('/Users/gfidarov/Desktop/crosscheckmmm/Sheet01', 'w')
data = csv.reader(input_file)
writer = csv.writer(output_file, quoting=csv.QUOTE_NONE, dialect='excel')
specials = ','

for line in data:
    line = [value.replace(specials, '') for value in line]
    print(line)
    writer.writerow(line)

input_file.close()
output_file.close()

When I try to do it I have this error _csv.Error: need to escape, but no escapechar set

Maybe you should try to consider each line as a string in order to replace/delete a character in it:

import csv
import string

input_file = open('/Users/gfidarov/Desktop/crosscheckmmm/Sheet1', 'r')
output_file = open('/Users/gfidarov/Desktop/crosscheckmmm/Sheet01', 'w')
data = csv.reader(input_file)
writer = csv.writer(output_file,quoting=csv.QUOTE_ALL)# dialect='excel')
specials = ','

for line in data:
    line = str(line)
    new_line = str.replace(line,specials,'')
    writer.writerow(new_line.split(','))

input_file.close()
output_file.close()

For the problem with escapechar, you can temporarily set an escapechar, then unset it after all your processing:

writer = csv.writer(output_file,quoting=csv.QUOTE_ALL, escapechar='\\')# dialect='excel')

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