且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-18 22:44:04

因为CSV文件基本上是文本文件,数据输入和再写出。没有其他方法来扩展列数,因为数据基本上是行主。最简单的方法来做你想说的话(毕竟,30k对不是这么多)是使用 csv package 来做解析工作。这个代码可以做你正在寻找...

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

你***使用数据库。 metakit sqlite3 与Tcl工作得很好,并能很好地处理这种任务。

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.