添加新的“列” Tcl 中的 csv 数据文件

发布于 2024-08-31 01:53:54 字数 410 浏览 0 评论 0原文

我正在处理“大”测量数据,大约 30K 键值 对。测量有迭代次数。每次迭代后 创建具有 30K 关键值对的数据文件(非 csv)。我想以某种方式 创建一个以下形式的 csv 文件:

Key1,value of iteration1,value of iteration2,...
Key2,value of iteration1,value of iteration2,...
Key2,value of iteration1,value of iteration2,...
...

现在,我想知道添加每次迭代测量的有效方法 Tcl 中将数据作为列保存到 csv 文件。到目前为止,无论哪种情况我似乎 需要将整个 csv 文件加载到某个变量(数组/列表)中并继续工作 每个元素都添加新的测量数据。这看起来效率有些低。 也许还有其他方法吗?

I am dealing with a "large" measurement data, approximately 30K key-value
pairs. The measurements have number of iterations. After each iteration a
datafile (non-csv) with 30K kay-value pairs is created. I want to somehow
creata a csv file of form:

Key1,value of iteration1,value of iteration2,...
Key2,value of iteration1,value of iteration2,...
Key2,value of iteration1,value of iteration2,...
...

Now, I was wondering about efficient way of adding each iteration mesurement
data as a columns to csv file in Tcl. So, far it seems that in either case I
will need to load whole csv file into some variable(array/list) and work on
each element by adding new measurement data. This seems somewhat inefficient.
Is there another way, perhaps?

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

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

发布评论

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

评论(1

已下线请稍等 2024-09-07 01:53:54

由于 CSV 文件本质上是文本文件,因此您必须加载所有数据并再次将其写出。由于数据基本上是行优先的,因此没有其他方法可以扩展列数。做你想做的事的最简单方法(毕竟,30k 对并没有那么多)是使用 csv 包 来完成解析工作。这段代码可能会满足您的需求……

package require csv
package require struct::matrix

# Load the file into a matrix
struct::matrix data
set f [open mydata.csv]
csv::read2matrix $f data , auto
close $f

# Add your data
set newResults {}
foreach key [data get column 0] {
    lappend newResults [computeFrom $key]; # This is your bit!
}
data add column $newResults

# Write back out again
set f [open mydata.csv w]
csv::writematrix data $f
close $f

不过,您可能最好使用数据库。 metakitsqlite3 与 Tcl 配合得很好,并且可以很好地处理此类任务。

Because CSV files are fundamentally text files, you have to load all the data in and write it out again. There's no other way to expand the number of columns since the data is fundamentally row-major. The easiest way to do what you say you want (after all, 30k-pairs isn't that much) is to use the csv package to do the parsing work. This code might do what you're looking for…

package require csv
package require struct::matrix

# Load the file into a matrix
struct::matrix data
set f [open mydata.csv]
csv::read2matrix $f data , auto
close $f

# Add your data
set newResults {}
foreach key [data get column 0] {
    lappend newResults [computeFrom $key]; # This is your bit!
}
data add column $newResults

# Write back out again
set f [open mydata.csv w]
csv::writematrix data $f
close $f

You would probably be better off using a database though. Both metakit and sqlite3 work very well with Tcl, and handle this sort of task well.

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