【问题标题】:TCL Calculating the length of numbers and formatting properly in text fileTCL计算文本文件中数字的长度和格式正确
【发布时间】: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

【问题讨论】:

    标签: string file tcl output


    【解决方案1】:

    您需要使用format 命令来格式化显示并使用regexp 来检索每行中的数字。

    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"]
    
     #puts $inputList
    
     foreach line $inputList { 
    
        # Extracting all the numbers in a line
        set numbers [ regexp -inline -all {\d+} $line ] 
        # Printing each numbers into the file
        foreach num $numbers {
            puts -nonewline  $FileOutput  "[ format "%-8d" $num ]"
        }
        puts  $FileOutput  ""; # This is just for the newline character
    } 
    close $filename 
    close $FileOutput
    

    format 命令中使用的- 指定转换后的参数应在其字段中左对齐。数字8 指定每个字段的宽度。

    参考:format

    更新 1:

    可以有很多方法。我们在列表numbers 的特定行中有完整的数字列表。之后我们使用foreach 遍历列表。在这里,您可以使用[lindex $numbers 1] 仅获取第二个元素,而不是循环所有元素。

    或者,由于我们知道元素是用空格分隔的,所以我们可以直接将其分配给一个列表并从中提取第二个元素,而不是使用这些方式。这完全取决于您的要求。

    【讨论】:

    • 我使用了您的提示,但输出错误.. 请参阅第一篇文章的编辑。
    • 您是否尝试过将输入保存在名为 input.txt 的文件中?另外,发布您的完整代码。
    • 我运行了你的脚本,它可以工作。但是,如果我的输入文件有 3 列数字,而我只想提取第二列的数字呢??
    • 使用scanstring range 处理输入可能更容易。这取决于确切地输入数据是什么。
    • #Dinesh。很抱歉进行了这么多编辑,但我无法打开新问题。无论如何,我能够自己提取每一列。但是现在我绑定按钮时遇到了一个奇怪的问题。
    猜你喜欢
    • 2015-02-20
    • 2013-03-19
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 2013-08-04
    相关资源
    最近更新 更多