首页/文章/ 详情

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

1年前浏览286

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

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

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

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

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

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

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

运行脚本

输出结果正确。

以下为脚本代码部分:

proc readCSV { channel { header 1 } { symbol , }} {
set quote 0set 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便得到配置表中的值了。

来源:NX网
UGPLM
著作权归作者所有,欢迎分享,未经许可,不得转载
首次发布时间:2022-11-09
最近编辑:1年前
老叶
本科 签名征集中
获赞 135粉丝 175文章 549课程 0
点赞
收藏
未登录
还没有评论

课程
培训
服务
行家

VIP会员 学习 福利任务 兑换礼品
下载APP
联系我们
帮助与反馈