[脚本分享][后处理下载]从CSV文件中读取数据——[配置表]的应用-第二期
上期回顾让后处理修改变得简单点——[配置表]的应用-第一期http://www.ugsnx.com/thread-287824-1-1.html(出处: UG爱好者)我们也许会觉得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 0
append row_temp $line
set row_temp [ split $row_temp , ]
foreach section $row_temp {
set quote [ expr { $quote + [ regexp -all \" $section ]}]
if { [ expr { $quote % 2 }] == "0" } {
append cell_temp $section
set cell_temp [ regsub {"(.*)"} $cell_temp {\1} ]
lappend cell $cell_temp
unset cell_temp
set quote 0
} else {
append cell_temp $section$symbol
}
}
lappend final [ regsub -all {""} $cell \" ]
unset cell
unset row_temp
} else {
append row_temp $line\n
}
}
# generate array if needed, or return $final here
set 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 ]/config.csv {RDWR} ]
array set csvData [ readCSV $csv 0 ]
puts $csvData(5,2)
puts $csvData(6,2)
close $csv
当我们在后处理中source脚本后,调用数组$csvData便得到配置表中的值了。得到的值该如何去利用,可以参考上一期的贴子。
以下为后处理和脚本文件
做一件对行业有贡献的事情。
本帖最后由 安周 于 2020-6-28 16:14 编辑
沙发拿下,希望大师继续分享!有你越来越精彩。 牛哥66666666666666 感谢分享,能出个后处理程序单就好了..{:smile:}
现在外挂的那些时间和刀长都不准确{:cry:} 楼主太强大了!!!!!!!!!!!!!!!!!。 楼主太强大了!!!! 大师666 感谢分享,有你越来越精彩。 多谢分享 楼主太强大了,感谢。
页:
[1]
2