分享

UG后处理从CSV文件中读取数据(配置表的应用)

 乙甲壬 2020-07-06
UG后处理从CSV文件中读取数据(配置表的应用)

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

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

UG后处理从CSV文件中读取数据(配置表的应用)

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

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

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

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

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

运行脚本

UG后处理从CSV文件中读取数据(配置表的应用)

输出结果正确。

以下为脚本代码部分:

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多