TCL脚本重组CSV文件数据

发布于 2025-01-22 18:09:25 字数 263 浏览 1 评论 0原文

我是TCL脚本的新手。

我的CSV文件中有2列。

示例:我的数据

A 12
D 18
B 33
A 11
D 49

我想使用第1列数据提取2列值。

所需的输出:


A: 12,11
B: 33
D: 18, 49

有人可以帮助我编写TCL脚本以执行此任务吗? 先感谢您。

I am new to tcl scripting.

I have 2 columns in my CSV file.

Example: My data

A 12
D 18
B 33
A 11
D 49

I would like to extract column 2 values using Column 1 data.

Required output:


A: 12,11
B: 33
D: 18, 49

Can someone please help me to write a tcl script to perform this task?
Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光倒影 2025-01-29 18:09:25

这是一个众所周知的问题,TCL的dict lappend正是您需要解决的工具(与csv :: Split从TCLLIB一起进行解析输入数据)。

package require csv

# Read split and collect
set data {}
while {[gets stdin line] >= 0} {
    lassign [csv::split $line] key value; # You might need to configure this
    dict lappend data $key $value
}

# Write out
puts ""; # Blank header line
dict for {key values} $data {
    puts [format "%s: %s" $key [join $values ", "]]
}

以上写为stdin到stdout滤镜。适应与文件一起工作是作为练习。

This is a very well known problem, and Tcl's dict lappend is exactly the tool you need to solve it (together with csv::split from tcllib to do the parsing of your input data).

package require csv

# Read split and collect
set data {}
while {[gets stdin line] >= 0} {
    lassign [csv::split $line] key value; # You might need to configure this
    dict lappend data $key $value
}

# Write out
puts ""; # Blank header line
dict for {key values} $data {
    puts [format "%s: %s" $key [join $values ", "]]
}

The above is written as a stdin-to-stdout filter. Adapting to work with files is left as an exercise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文