2

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 Answers 4

5

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)

Sign up to request clarification or add additional context in comments.

Comments

2

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

3 Comments

It will be quoting too much. Not sure how much the difference the size will be, but my csv output can be quite big (updated the question with more details). And quoting can be unnecessary sometimes.
The change in file size due to removing some quotation marks will be trivial compared to pretty much anything else you could do (such as compressing the file and reading it through a decompression stream). It sounds like you've got a major case of the XY problem.
Good point. I don't know, I guess you're right. But it seems like the "QUOTE_MINIMAL" could have been more flexible. Even with custom quoting way from the other answer is too hacky.
0

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

Comments

0

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

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

1 Comment

does not work. I get _csv.Error: need to escape, but no escapechar set

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.