您可能想要拆分,因此您还可以对 csv 行执行额外检查。在default csv package 的帮助下,我会建议如下内容:
package require csv
set input {"522","-1","12345678","12345678","Completed","","","","height 5' 5" and weight 170lbs","","","9876543","ABCD","2016-06-12T23:54:00-05:00","2016-06-12T23:59:00-05:00"}
# Quick check the line, if it's not complete...
if {![::csv::iscomplete $input]}
# Replace all 'quote comma quote' with null character (or some other character you are certain is
# not in your csv line
set intermediate [regsub -all {","} $input \0]
# Split on null character (or the character you picked on the previous line)
set columns [split $intermediate \0]
# Ensure that the csv line contains the expected number of columns
if {[llength $columns] == 15} {
# Replace the quotes in each element of the list of columns
set columns [lmap x $columns {string map {{"} {}} $x}]
} else {
# Do further checks otherwise to see what's wrong
}
puts "Split data:"
puts $columns
} else {
set columns [::csv::split -alternate $input]
}
set output [::csv::join $columns "," always]
puts "\nRejoined (if you still need it):"
puts $output
输出:
Split data:
522 -1 12345678 12345678 Completed {} {} {} {height 5' 5 and weight 170lbs} {} {} 9876543 ABCD 2016-06-12T23:54:00-05:00 2016-06-12T23:59:00-05:00
Rejoined (if you still need it):
"522","-1","12345678","12345678","Completed","","","","height 5' 5 and weight 170lbs","","","9876543","ABCD","2016-06-12T23:54:00-05:00","2016-06-12T23:59:00-05:00"
这样您就可以更好地控制正在发生的事情,并可能发现 csv 的任何其他问题。