Quote character TCL CSV

how to add citation character using csv:

% package require csv 0.7.3 % set l [list "21" "Some test" "Some test"] % ::csv::join $l {,} {"} 21,Some test,Some test 

I want "21", "Some test", "Some test"

+4
source share
3 answers

The csv package does not provide such quoting unless you cheat on it.

It is safe to say that it should simply provide the opportunity to do this, but try this instead:

 package require struct::list set l {1 "Some Text" "Some Test"} set r [join [struct::list map $l {format {"%s"}}] ","] puts $r 
+2
source

The trick is this:

  • Make Tcl not separate double quotes;
  • Disable csv escaping (default is " )
 % set L [list {"21"} {"Some test"} {"Some test"}] {"21"} {"Some test"} {"Some test"} % csv::join $L , {} "21","Some test","Some test" 

Please note that this does not solve the problem with one of your values ​​having the symbol " somewhere in the middle - if this can happen, you should somehow avoid the symbols that your target CSV processor understands.

0
source

This solution is not the best, but it works. There may be corner cases that you need to observe. It does not require changing items in the list and does not need a CSV package.

 set theList [list "21" "Some test" "Some test"] set quotedCSV "\"[join $theList {","}]\"" puts $quotedCSV 

Basically, quotedCSV = quote + ( theList, separated by quote+comma+quote ) + quote . My suggestion is to go with CSV if you really don't need quotes.

0
source

Source: https://habr.com/ru/post/1434415/


All Articles