ns_getcsv - Parse CSV Files
This command parses lines of CSV files.
Read a single tuple (line) from an open CSV file (denoted by fileid) and parse the results into the Tcl list variable named in the last argument (varname). The command returns the number of parsed fields or -1 on end of file.
Example script from regression test:
set csvFile test.csv set f [open $csvFile] while {1} { set r [ns_getcsv -trim $f line] switch $r { -1 {break} 0 {} default {lappend lines $line} } } close $f set lines
Result:
{aaa bbb ccc} {aaa {b bb} ccc} {zzz yyy xxx} {1 2 3} {includes(\")quote {quoted includes (") quote}} {elm2 {} empty} single {last3 empty {} {} {}}}
sample.cvs
"aaa","bbb","ccc" "aaa","b bb","ccc" zzz,yyy,xxx 1 , 2 , 3 includes(")quote,"quoted includes ("") quote" elm2,,empty single last3,empty,,,