Along the lines of my previous question, how can i join a list of strings into a string such that values get quoted cleanly. Something like:
['a', 'one 'two' three', 'foo, bar', '''both''''']
into:
a, 'one 'two' three', 'foo, bar', 'both\'''
I suspect that the csv module will come into play here, but i’m not sure how to get the output I want.
Using the
csvmodule you can do that way:If you need a string just use
StringIOinstance as a file:The output:
a,'one ''two'' three','foo, bar','both''''csvwill write in a way it can read back later. You can fine-tune its output by defining adialect, just setquotechar,escapechar, etc, as needed:The output:
a,one \'two\' three,'foo, bar',both\''The same dialect can be used with csv module to read the string back later to a list.