2
votes

My data looks something like this:

data = [
  [" trailing space", 19, 100],
  [" ", 19, 100],
]

writer = csv.writer(csv_filename, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

Output

 trailing space,19,100
 ,19,100

What I want

" trailing space",19,100
" ",19,100

Python default CSV writer has the option to "QUOTE_MINIMAL" but it doesn't include quoting strings with extra spaces in it. In my case, those empty spaces are actually critical, but without quoting, the reader (like libre-office) strips the spaces if not quoted.

Is there any built in options or quick cheap way to tell the writer to quote empty strings with spaces?

Also, "QUOTE_NONNUMERIC" is quoting too much. The actual data is huge ( few hundred megabytes with 60% - 70% of strings). It may sounds silly, but I'm trying to reduce the csv size by minimizing the quotes.

4

4 Answers

3
votes

It's a bit of a hack but one way of achiving this could be

df.to_csv(quoting=csv.QUOTE_MINIMAL, escapechar=' ')

It's not document but QUOTE_MINIMAL seems to quote fields containing escapechar although it has no effect (as quoting is not NONE and doublequote is True by default)

2
votes

Why not just use QUOTE_NONNUMERIC? That'll quote all strings, not just those with spaces, but it'll certainly quote those too.

with open("quote.csv", "w", newline="") as fp:
    writer = csv.writer(fp, quoting=csv.QUOTE_NONNUMERIC)
    writer.writerows(data)

gives me

(3.5.1) dsm@notebook:~/coding$ cat quote.csv 
" leading space",19,100
" ",19,100
0
votes

Have you tried csv writer in Python with custom quoting

Though make sure you know what you are quoting and take to manually escape stuff

0
votes

Try removing the quoting altogether. Will keep all quote characters as required.

writer = csv.writer(csv_filename, delimiter=',', quoting=csv.QUOTE_NONE)