如何从tcl中的文件中提取特定值
行与行之间没有空格$$在这种情况下,正则表达式或字符串格式会有所帮助。然而,目前还不清楚给定样本中的文件格式是什么;很难准确地说出哪些比特是有趣的,特定片段的变化范围是什么,等等。不过,我们可以采取一些步骤:如何从tcl中的文件中提取特定值,tcl,Tcl,行与行之间没有空格$$在这种情况下,正则表达式或字符串格式会有所帮助。然而,目前还不清楚给定样本中的文件格式是什么;很难准确地说出哪些比特是有趣的,特定片段的变化范围是什么,等等。不过,我们可以采取一些步骤: $$ comments //not needed $$ comments /not needed $$ $$ $$ comments //not needed $$.INPUT a vcc
$$ comments //not needed
$$ comments /not needed
$$
$$
$$ comments //not needed
$$.INPUT a vcc // needed
$$.OUTPUT o //needed
$$.sdsds
$$
$$.sdsds
$$
$$.sdsdsds
Mg1.qna o a vss vss n 0.36 0.03 mult=4 $$nnn //needed
Mg1.qpa o a vcc vcc p 0.36 0.03 mult=6 $$nnn //needed
使用它:
proc parseFileContents {contents infoVar} {
upvar 1 $infoVar inf
set lineNum 0
foreach line [split $contents "\n"] {
incr lineNum
# Skip comment lines (?)
if {[string match {$*} $line} continue
# Skip blank lines
if {[string trim $line] eq ""} continue
# Parse a "real" line
if {[scan $line "%s%s%s%s%s%s%f%f%s%s" a b c name d e value f g h] == 10} {
set inf($name) $value
} else {
# Oh dear, didn't work!
puts "warning: did not understand line $lineNum\n$line"
}
}
}
如前所述,正则表达式也可以用于解析数据行,使用
regexp
而不是scan
来进行匹配,但我对格式的理解还不够透彻,无法说出要使用什么。项目中有几个库可以处理纯文本文件,我建议您看看
如果你坚持自己写一本,你可以用这样的东西:
parseFileContents $theContentsOfTheFile data
puts "Keys: [array names data]"
puts "VSS: $data(vss)"
puts "VCC: $data(vcc)"
编辑:但请看多纳尔的答案,因为我发现它比我自己的更好。@DKF-我在此承诺不会再发布关于Tcl的其他答案:-)你总是在这方面击败我,并在这方面写更好的答案。我们都感谢您的时间和知识-非常感谢@Nir:practice:-)+1和尊重两者,这个问题在我看来像是胡言乱语,更不用说解决它了。
set fd [open $filename r]
while { [gets $fd line] >= 0 } {
set data [split $line]
if { [lindex $data 0] == {$} } {
continue; #this is a comment line
}
puts [lindex $line 3]; #or whatever index you need...
}
close $fp