output - 创建具有正确索引的文件
问题描述
我目前有一个包含 300 行和 3 列的文本文件。
我需要遍历所有行,用 3 列中的值分配 3 个变量。
每次循环通过 for 循环时,我都需要生成一个输出文件,其索引号在输出文件的名称中。(也就是说,我总共需要产生300个输出文件)
例如,对于第 49 次迭代,我需要生成一个名为product-49.out的输出文件。你对如何做到这一点有什么建议吗?
解决方案
这是一个如何做到这一点的例子:
set counter 1
set fp_in [open my-text-file.txt r]
set line [gets $fp_in]
while { ![eof $fp_in] } {
# store the columns on the current line in variables:
lassign $line var1 var2 var3
set fp_out [open "product-$counter.out" w]
# write one line from the input file to each output file:
puts $fp_out $line
close $fp_out
set line [gets $fp_in]
incr counter
}
close $fp_in
推荐阅读
- django - 如何在 vue.js 和 django rest 框架中访问经过身份验证的登录用户
- html - 如何将图像放在文本旁边因为我想将图像放在一些文本旁边
- flutter - Open flutter screen from Native Method call
- python-asyncio - 如何在 Python 中将基于回调的库与 asyncio 库结合起来?
- typescript - 如何在 atom 中正确显示 emacs 编辑的文件?
- reactjs - Jest 使用 create-react-app 手动模拟:如何在测试中使用模拟值
- javascript - 如何在特定 laravel 刀片视图上扩展 vuejs 应用程序实例
- javascript - jQuery从包含的脚本中最近的按钮数据标签中获取
- rxjs - 当内部的任何一个发出时,发出 combine observable
- iot - 解码 lorawan 数据 gps 追踪器 Moko LW001-BG Thethings network