csvtk icon indicating copy to clipboard operation
csvtk copied to clipboard

Add ability to quote all output

Open moorereason opened this issue 11 months ago • 3 comments

The Problem

I need to be able to quote all headers and values in the output in order to maintain compatibility with another system. AFAIK, csvtk doesn't currently support his.

Reproduction

Given input file x.csv:

"A A","B","C","D"
"1","  2","3  ","4"

Given command:

csvtk sort x.csv

csvtk outputs:

A A,B,C,D
1,"  2",3  ,4

The desired output should be the same as the input file.

Workaround

I'm currently using miller as a workaround:

csvtk sort x.csv | mlr --csv --quote-all --no-color cat

moorereason avatar Jan 11 '25 18:01 moorereason

Sorry, csv.Writer does not support that.

shenwei356 avatar Jan 12 '25 06:01 shenwei356

Here is a workaround using only csvtk. Not sure how general this is but it works on the sample x.csv file. It inserts (and then removes) quotes at the beginning of each field since csvtk will always quote fields with embedded quotes.

Suppose this is x.csv

"A A","B","C","D"
"1","  2","3  ","4"
"1"," "" 2","3  ","4"

Then using Windows cmd line (escapes and continuation character are slightly different in Linux but it should work if you make the corresponding modifications) try the following:

  • sort
  • replace all non-separator commas with ! (or replace with any character not in the input file)
  • delete quotes
  • insert a doubled quote at the beginning of each field
  • remove the doubled quote
  • revert the ! characters back to comma

If it were known that there are no embedded commas, such as the test file above, then the first and last replace lines can optionally be omitted.

csvtk sort x.csv | ^
csvtk replace -f 1- -p , -r ! | ^
csvtk del-quotes | ^
csvtk -H -l replace -f 1- -p "^" -r """" | ^
csvtk -H -l replace -f 1- -p "^." -r "" | ^
csvtk replace -f 1- -p ! -r , 

giving

" A A"," B"," C"," D"
" 1","  "" 2"," 3  "," 4"
" 1","   2"," 3  "," 4"

ggrothendieck avatar Mar 29 '25 02:03 ggrothendieck

replace all non-separator commas with ! (or replace with any character not in the input file)

Impossible in the general case unfortunately!

tseemann avatar Nov 13 '25 02:11 tseemann