【发布时间】:2014-12-09 08:05:24
【问题描述】:
我需要处理具有 3 列值的文本文件,如下所示:
10 650 8456
1 3264 64643
...
现在我有以下问题:
1) 我不知道如何计算每个数字的长度(例如:10 = 2 个数字;650 -> 3 个数字;64643 -> 5 个数字)
-
2) 一旦解决了第一点,我需要创建一个具有适当数据格式的输出 txt 文件,如下所示:
|--01--||--02--||--03--|
每列有 8 个空格可用于写数字;例如,如果一个数字有 4 个值,例如 8456,我想数一数 其他 4 个空格 (8 - 4) 仍然存在,然后在第 9 个空格写在第二列,另一个数字等等.. 这里是一个欲望输出的例子:
|--01--||--02--||--03--|
10 650 8456
1 3264 64643
这是我的一段代码,但我不知道如何计算数字并在第一个数字之后写其他数字。
set FileOutput [open $Output w]
set FileInput [open $filename r]
set filecontent [read $FileInput]
set inputList [split $filecontent "\n"]
puts $FileOutputGas " [lindex $inputList 3] [lindex $inputList 4] [lindex $inputList 5]"
但是通过这种方式,我始终保持相同的文本格式,数字之间有固定的空格;相反,我想动态放置空格。
编辑:以这种方式输出错误:
set formatStr {%-8d}
puts $FileOutputGas "[format $formatStr [lindex $num 3]]"
它打印出格式“-8d”而不是数字
编辑 2:绑定按钮时出现输出问题。 我之前提到的问题是由于按了一个按钮。如果我运行你的脚本,我不知道为什么输出是正确的,但是如果我在一个按钮中插入所有这些操作,它会以这种方式给我一个错误的输出:
button .bCreate -text "CREATE OUTPUT" -width 30 -height 5 -activebackground green -font " -12"
bind .bCreateGas <1> {
set Output "output.txt"
set filename "input.txt"
set FileOutput [open $Output w]
set FileInput [open $filename r]
set filecontent [read $FileInput]
set inputList [split $filecontent "\n"]
set CtriaFind [lsearch -all -inline $inputList CTRIA3*]
foreach line $CtriaFind {
# Extracting all the numbers in a line
set numbers [ regexp -inline -all {\d+} $line ]
set num3 [lindex $numbers 3]
set num4 [lindex $numbers 4]
# Printing each numbers into the file
puts -nonewline $FileOutput " [ format "%-8d" $num3] [ format "%-8d" $num4]"
puts $FileOutput "";
}
}
input.txt 文件的一部分是这个:
GRID 48588 -.366712-3.443-2.3697197
GRID 48606 -.366683-.0373640.374481
GRID 48607 -.366536-3.888-2.3767999
GRID 48608 -.366735-3.589-2.3721335
$$
$$ SPOINT Data
$$
CTRIA3 101268 0 9793 4098 9938
CTRIA3 101353 0 3986 9928 3803
CTRIA3 101363 0 4010 12337 3932
我只想打印
9793 4098
3986 9928
4010 12337
【问题讨论】: