TCL-在CSV文件中插入一行



如果有人能帮我,我会非常高兴。

使用Tcl脚本,当StringId增加时,我想在csv文件中插入一行。最终数据如下:

StringId、X、Y、Z、矿坑、路堑、倾卸场、道路类型、阶段、说明14502.9906247862611700.27127890071351.70766376371,Main,m12,,Ramp,,14516.27714475509711659.22151179831355.04407601514,Main,m12,,Ramp,,24573.8811115.0961349.655,Main,m11,,Surface,,24579.09111005.0271340.086,Main,m11,,Surface,,34590.81810957.1541335.894,Main,m11,,Surface,,34624.94108868.6381327.38,Main,m11,,Surface,,44672.86510803.071320,Main,m11,,Surface,,44720.1710757.5451320.894,Main,m11,,Surface,

我想通过在新行中插入0,0,0,0将其更改为以下内容:

StringId、X、Y、Z、矿坑、路堑、倾卸场、道路类型、阶段、说明14502.9906247862611700.27127890071351.70766376371,Main,m12,,Ramp,,14516.27714475509711659.22151179831355.04407601514,Main,m12,,Ramp,,

0,0,0,0,

24573.8811115.0961349.655,Main,m11,,Surface,,24579.09111005.0271340.086,Main,m11,,Surface,

0,0,0,0

34590.81810957.1541335.894,Main,m11,,Surface,,34624.94108868.6381327.38,Main,m11,,Surface,

0,0,0,0,

44672.86510803.071320,Main,m11,,Surface,,44720.1710757.5451320.894,Main,m11,,Surface,

在这种情况下,我们不需要完全解析文件。相反,我们可以使用lindex [split $line ","] 0获取第一列,并将其余列保留为未解析列。唯一的另一个技巧是,我们需要将列名作为它们自己的特殊事物来处理,在我们吸收其余数据之前读取它们。

set f [open $inFilename]
set columnNames [gets $f]
set data [split [read $f] "n"]
close $f
# In theory, we should check which column is StringId with [lsearch],
# but I hard code it to 0 for convenience
set f [open $outFilename w]
puts $f $columnNames
foreach line $data {
set id [lindex [split $line ","] 0]
# If we've changed the current ID, print the stuff to insert
if {[info exist current] && $current != $id} {
puts $f "n0,0,0,0,n"
}
# Remember the current ID
set current $id
puts $f $line
}
close $f

相关内容

  • 没有找到相关文章

最新更新