UG后处理配置表的应用之二:从CSV文件中读取数据

上期分享了从txt文件中获取数据,从而完成配置表。 我们也许会觉得txt格式的配置表不够清爽,蜗牛是带有一点点强迫症的。 如果我们做成csv格式的呢?

上期分享了从txt文件中获取数据,从而完成配置表:https://www.ugnx.net/fx/688

我们也许会觉得txt格式的配置表不够清爽,蜗牛是带有一点点强迫症的。

如果我们做成csv格式的呢?

attachments-2020-07-9NC8ulUk5f01dd45dcdf0.jpg

这样看着会舒服很多吧 :D

那么怎么从后处理获取csv文件中的数据呢?


我们可以用过一个脚本达到目的。

在表格中,设1A列的单元格坐标为(0,0

需要获取的值为6C列,即(5,2);7C列,即(6,2

运行脚本

attachments-2020-07-WGJQRt0w5f01dd66b09e0.jpg

输出结果正确。


以下为脚本代码部分:

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 [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便得到配置表中的值了。

得到的值该如何去利用,可以参考上一期的贴子。

 

以下为后处理和脚本文件

 后处理和脚本文件.rar

做一件对行业有贡献的事情。

  • 发表于 2020-07-02 13:54
  • 阅读 ( 4747 )
  • 分类:NX后处理

2 条评论

请先 登录 后评论
闪光蜗牛
闪光蜗牛

2 篇文章

作家榜 »