简体   繁体   English

Tcl 使用 csv 写入文件

[英]Tcl to write a file using csv

I need help writing a tcl, which reads portions of data from a csv file and write into a text file in the following pattern.我需要帮助编写一个 tcl,它从 csv 文件中读取部分数据并按照以下模式写入文本文件。

NAME : FROM= -100 -346 -249 -125 TO= -346 -249 -125 100 COLOR= COLOR1 COLOR2 COLOR3 COLOR4

NAME will be a fixed row, NAME 将是一个固定的行,

FROM and TO information should be retreived from csv file and FROM 和 TO 信息应该从 csv 文件和

COLOR information can be hardcoded array of colors from the Tcl itself. COLOR 信息可以是来自 Tcl 本身的硬编​​码颜色数组。

From csv data below, the first value(-100) under MIN will be the first value(-100) under FROM of text file.从下面的csv数据中,MIN下的第一个值(-100)将是文本文件FROM下的第一个值(-100)。 The last value(100) from excel MAX column will be the last value(100) under text file TO column. excel MAX 列中的最后一个值(100)将是文本文件 TO 列下的最后一个值(100)。 The values under VALUE column in excel will be rounded and used as TO and FROM per pattern shown. excel 中 VALUE 列下的值将被四舍五入并用作每个显示模式的 TO 和 FROM。

Data    VALUE   
100 -345.8756   
200 -249.3654   
300 -125.3554   
COUNT   MIN MAX
1   -100    -98
93  84  86
98  94  96
99  96  98
100 98  100

在此处输入图片说明

Some pointers:一些提示:

  • reading lines from a file从文件中读取行
  • use set fields [regexp -inline -all {\\S+} $line] to split the line into words separated by arbitrary whitespace使用set fields [regexp -inline -all {\\S+} $line]将行拆分为由任意空格分隔的单词
  • you'll need to keep a couple of boolean flags as a state machine to determine what to do with the current line (are you collecting values or looking for the min/max)您需要保留几个布尔标志作为状态机,以确定如何处理当前行(您是在收集值还是在寻找最小值/最大值)
  • use [expr {round([lindex $fields end])}] to round the values: see https://tcl.tk/man/tcl8.6/TclCmd/expr.htm#M22使用[expr {round([lindex $fields end])}]对值进行舍入:参见https://tcl.tk/man/tcl8.6/TclCmd/expr.htm#M22

See if that gets you started.看看这是否能让你开始。

package require struct::matrix
package require csv
package require fileutil

array set OPTS { 
    csv_input_filename    output/stackoverflow.csv 
    txt_output_filename   output/stackoverflow.txt
    colors                {COLOR1 COLOR2 COLOR3 COLOR4}
}

set output_format {NAME : 
FROM= %s %s %s %s
TO= %s %s %s %s
COLOR= %s
}

try {::struct::matrix xdata} on error {} {xdata destroy; ::struct::matrix xdata}
set chan [open $OPTS(csv_input_filename)]
csv::read2matrix $chan xdata , auto
close $chan
csv::joinlist [xdata get rect 0 0 end end]

fileutil::writeFile $OPTS(txt_output_filename) \
 [format $output_format [xdata get cell 1 5] \
 [expr {round([xdata get cell 1 1])}] [expr {round([xdata get cell 1 2])}] \
 [expr {round([xdata get cell 1 3])}] [expr {round([xdata get cell 1 1])}] \
 [expr {round([xdata get cell 1 2])}] [expr {round([xdata get cell 1 3])}] \
 [xdata get cell 2 9] [list {*}$OPTS(colors)]]
xdata destroy

fileutil::cat [file native $OPTS(txt_output_filename)]
# NAME :
# FROM= -100 -346 -249 -125
# TO= -346 -249 -125 100
# COLOR= COLOR1 COLOR2 COLOR3 COLOR4

Note:注意:

The script should result to the expected output .txt file, assuming the .csv file is contained within the output subfolder from within the current running directory of course.脚本应该产生预期的输出 .txt 文件,当然,假设 .csv 文件包含在当前运行目录中的输出子文件夹中。

Assuming the script filename is 'matrix_csv_extract.tcl' you would simply source the tcl script interactively to have it run:假设脚本文件名是“matrix_csv_extract.tcl”,您只需以交互方式获取 tcl 脚本以使其运行:

source matrix_csv_extract.tcl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM