I'm looking for a way to define custom quoting
with csv.writer
in Python. There are 4 built-in ways to qoute values:
csv.QUOTE_ALL, csv.QUOTE_MINIMAL, csv.QUOTE_NONNUMERIC, csv.QUOTE_NONE
However I need a quoting mechanism which will emulate Postgres' FORCE QUOTE *
, i.e. it will quote all non-None values. With csv.QUOTE_ALL
Python will turn None into ''
but I would like to have empty string instead.
Is it possible to do that with built-in csv
module ( I'm not interested in hacks, I'm already doing that :P )? Or am I forced to write/get some custom csv parser?
And generally: is it possible to write custom quoting mechanism for csv
module?
Disable
csv
quoting and add the quotes yourself:By setting both
escapechar
andquotechar
to empty strings you avoid the module quoting your already-quoted values.The above works as long as you don't use the delimiter in the csv values.
Note that by this time it would just be easier to write comma-delimited lines yourself:
I've written my own csv writer which does exactly what I want:
If anyone sees any problem with it, then please let me know. I'm still looking for a solution with
csv
module though.