Reputation: 972
I have a row of data that I am trying to write to a .tab file that looks like this:
row = ['07/19/2017/10/33/30', 'gg_ca_18" dishwashers_204454792491_1t1',
Decimal('369.00'), 1L, '66745355']
I want to leave the single quote not escaped so that it looks like this:
07/19/2017/10/33/30 gg_ca_18" dishwashers_204454792491_1t1 369.00 1 66745355
I am writing to a file using this code:
with open(file_name, 'wb') as outfile:
a = csv.writer(outfile, delimiter='\t') # ,escapechar=None, quoting=csv.QUOTE_NONE
a.writerows(row)
I have tried using escapechar=None, quoting=csv.QUOTE_NONE
and several variations of this. I always get the error Error: need to escape, but no escapechar set
. What is the best way to not escape the single quote in the middle of the line? Is it possible using csv
module?
Upvotes: 2
Views: 3648
Reputation: 57033
If you do not want to escape anything, it is not a CSV file anymore. Convert each row item to a string, joint the strings into one string, and write it into the file:
line = ' '.join(map(str,row))
#'07/19/2017/10/33/30 gg_ca_18" dishwashers_204454792491_1t1 369.00 1 66745355'
outfile.write(line + "\n")
Upvotes: 1
Reputation: 8061
You can just use a different quotechar
:
with open(file_name, 'w') as outfile:
a = csv.writer(outfile, delimiter='\t', quotechar="'")
a.writerow(row)
Upvotes: 3
Reputation: 174
If you want to escape a character, use \
.
In you example, it would be \"
Upvotes: -2