我们也许会觉得txt格式的配置表不够清爽,我是带有一点点强迫症的。 如果我们做成csv格式的呢? 这样看着会舒服很多吧 :D 那么怎么从后处理获取csv文件中的数据呢? 我们可以用过一个脚本达到目的。 在表格中,设1行A列的单元格坐标为(0,0) 需要获取的值为6行C列,即(5,2);7行C列,即(6,2) 运行脚本 输出结果正确。 以下为脚本代码部分: proc readCSV { channel { header 1 } { symbol , }} {set quote 0 set data [ split [ read $channel nonewline ] '\n' ]foreach line $data {set quote [ expr { $quote + [ regexp -all \' $line ]}]if { [ expr { $quote % 2 }] == '0' } {set quote 0append row_temp $lineset row_temp [ split $row_temp , ] foreach section $row_temp {set quote [ expr { $quote + [ regexp -all \' $section ]}]if { [ expr { $quote % 2 }] == '0' } {append cell_temp $sectionset cell_temp [ regsub {'(.*)'} $cell_temp {\1} ]lappend cell $cell_tempunset cell_tempset quote 0} else {append cell_temp $section$symbol}}lappend final [ regsub -all {''} $cell \' ]unset cellunset row_temp} else {append row_temp $line\n}}# generate array if needed, or return $final hereset row [ llength $final ]set column [ llength [ lindex $final 0 ]]if { $header == 1 } {for { set i 0 } { $i < $row } { incr i } { for { set j 0 } { $j < $column } { incr j } {set csvData([ lindex [ lindex $final 0 ] $j ],$i) [ lindex [ lindex $final $i ] $j ]}}} else {for { set i 0 } { $i < $row } { incr i } { for { set j 0 } { $j < $column } { incr j } {set csvData($i,$j) [ lindex [ lindex $final $i ] $j ]}}}return [ array get csvData ]}set csv [ open [file dirname [info script]]/config.csv {RDWR} ]array set csvData [ readCSV $csv 0 ]#puts $csvData(5,2) #puts $csvData(6,2) close $csv 当我们在后处理中source脚本后,调用数组$csvData便得到配置表中的值了。 |
|