File TCl Tk如何从文件中捕获浮点数
我在处理一个包含浮点数的文件时发现一些问题。 以下是我的文件中的一些行:File TCl Tk如何从文件中捕获浮点数,file,floating-point,tcl,tk,File,Floating Point,Tcl,Tk,我在处理一个包含浮点数的文件时发现一些问题。 以下是我的文件中的一些行: 174259 1.264944 -.194235 4.1509e-5 174260 1.264287 -.191802 3.9e-2 174261 1.266468 -.190813 3.9899e-2 174262 1.267116 -.193e-3 4.2452e-2 我试图做的是找到我的欲望号所在的行(例如“174260”),并提取以下三个数字 这是我的代码: set Output [open "Outp
174259 1.264944 -.194235 4.1509e-5
174260 1.264287 -.191802 3.9e-2
174261 1.266468 -.190813 3.9899e-2
174262 1.267116 -.193e-3 4.2452e-2
我试图做的是找到我的欲望号所在的行(例如“174260”),并提取以下三个数字
这是我的代码:
set Output [open "Output3.txt" w]
set FileInput [open "Input.txt" r]
set filecontent [read $FileInput]
set inputList [split $filecontent "\n"]
set Desire 174260
set FindElem [lsearch -all -inline $inputList $Desire*]
set Coordinate [ regexp -inline -all {\S+} $FindElem ]
set x1 [lindex $Coordinate 1]
set y1 [lindex $Coordinate 2]
set z1 [lindex $Coordinate 3]
puts $Output "$x1 $y1 $z1"
使用字符串“{\S+}”的regexp方法,我获得一个花括号作为最后一个字符:
1.264287 -.191802 3.9e-2}
我不知道如何只提取数字值,而不提取整个字符串。在这种情况下,我很想使用最简单的选项
set Output [open "Output3.txt" w]
set FileInput [open "Input.txt" r]
set Desire 174260
while {[gets $FileInput line] >= 0} {
lassign [regexp -inline -all {\S+} $line] key x1 y2 z1
if {$key == $Desire} {
puts $Output "$x1 $y1 $z1"
}
}
close $FileInput
close $Output
如果做不到这一点,您的问题是您使用的是lsearch-all-inline
,它返回一个列表,然后使用regexp
将该列表作为字符串处理。您应该使用以下方法处理此问题:
foreach found $FindElem {
set Coordinate [ regexp -inline -all {\S+} $found ]
set x1 [lindex $Coordinate 1]
set y1 [lindex $Coordinate 2]
set z1 [lindex $Coordinate 3]
puts $Output "$x1 $y1 $z1"
}
这实际上不如一开始就正确理解行,一次只处理一行数据非常简单。在这种情况下,我真的很想使用最简单的选项
set Output [open "Output3.txt" w]
set FileInput [open "Input.txt" r]
set Desire 174260
while {[gets $FileInput line] >= 0} {
lassign [regexp -inline -all {\S+} $line] key x1 y2 z1
if {$key == $Desire} {
puts $Output "$x1 $y1 $z1"
}
}
close $FileInput
close $Output
如果做不到这一点,您的问题是您使用的是lsearch-all-inline
,它返回一个列表,然后使用regexp
将该列表作为字符串处理。您应该使用以下方法处理此问题:
foreach found $FindElem {
set Coordinate [ regexp -inline -all {\S+} $found ]
set x1 [lindex $Coordinate 1]
set y1 [lindex $Coordinate 2]
set z1 [lindex $Coordinate 3]
puts $Output "$x1 $y1 $z1"
}
这实际上不如一开始就正确理解行,一次只处理一行数据非常简单。在这种情况下,我真的很想使用最简单的选项
set Output [open "Output3.txt" w]
set FileInput [open "Input.txt" r]
set Desire 174260
while {[gets $FileInput line] >= 0} {
lassign [regexp -inline -all {\S+} $line] key x1 y2 z1
if {$key == $Desire} {
puts $Output "$x1 $y1 $z1"
}
}
close $FileInput
close $Output
如果做不到这一点,您的问题是您使用的是lsearch-all-inline
,它返回一个列表,然后使用regexp
将该列表作为字符串处理。您应该使用以下方法处理此问题:
foreach found $FindElem {
set Coordinate [ regexp -inline -all {\S+} $found ]
set x1 [lindex $Coordinate 1]
set y1 [lindex $Coordinate 2]
set z1 [lindex $Coordinate 3]
puts $Output "$x1 $y1 $z1"
}
这实际上不如一开始就正确理解行,一次只处理一行数据非常简单。在这种情况下,我真的很想使用最简单的选项
set Output [open "Output3.txt" w]
set FileInput [open "Input.txt" r]
set Desire 174260
while {[gets $FileInput line] >= 0} {
lassign [regexp -inline -all {\S+} $line] key x1 y2 z1
if {$key == $Desire} {
puts $Output "$x1 $y1 $z1"
}
}
close $FileInput
close $Output
如果做不到这一点,您的问题是您使用的是lsearch-all-inline
,它返回一个列表,然后使用regexp
将该列表作为字符串处理。您应该使用以下方法处理此问题:
foreach found $FindElem {
set Coordinate [ regexp -inline -all {\S+} $found ]
set x1 [lindex $Coordinate 1]
set y1 [lindex $Coordinate 2]
set z1 [lindex $Coordinate 3]
puts $Output "$x1 $y1 $z1"
}
这实际上不如一开始就正确地理解行,一次处理一行数据非常简单