Reputation: 605
Trying to open a CSV that contains quotes and remove them. I've looked through the documentation for the CSV module for python and it's removing the quotes like I want but the output is trying to escape the comma, which is what I'm trying to avoid. This code:
csv.register_dialect('escaped',delimiter=",", escapechar="\\", quoting=csv.QUOTE_NONE)
reader = csv.reader(open(input_file, "rb"), skipinitialspace=True)
writer = csv.writer(open(output_file, "wb"), dialect='escaped')
writer.writerows(reader)
Results in output like this value1\,value2. How do I get the backslash out of there so it's only the comma in the output. The input files contains a bunch of lines which look this:
"Value1,Value2"
Upvotes: 2
Views: 7365
Reputation: 1
try using '\7' as the escape char:
with open('dmy.tmp','w') as f:
writer=csv.writer(f,escapechar='\7',quoting=csv.QUOTE_NONE)
content=[['a','b','c'],[1,2,3]]
writer.writerows(content)
Upvotes: 0
Reputation: 12092
The behavior you are observing is inline with what the documentation says about QUOTE_NONE
If all you are trying to do is get rid of the quotes, and given that the input lines are as simple as what you mentioned in the comments, wouldn't this work?
with open('input') as in_file, open('output', 'w') as out_file:
for line in in_file:
out_file.write(line.replace('\"', ''))
Upvotes: 3