python - 用于读取文本文件并在 excel 中制表的 Python 程序
问题描述
0 0.303 0.775 测试集_0000.img
sm 0.524
1 0.670 0.670 测试集_0001.img
sm 2.670
我想阅读文本文件(上面的例子是 txt 文件中的数据)。我想读取大于 2.5 的 sm 值,并在 excel 表中将该 sm 值连同名称(写为 TestSet_ooxx.img)制成表格。
现在我可以读取整个文本文件并将其写入 xls 表。但我只需要在 Excel 表中写入少量信息。
解决方案
打开每个文件,遍历每一行。根据行拆分后的值附加到新文件。
with open("<FILENAME>.txt") as FileObj:
for line in FileObj:
x = line.strip().split()[1]
if (x > "2.5"):
file = open("<FILENAME>.csv","a")
file.write(line)
file.close
推荐阅读
- sql - 创建仅存储默认数据的表的替代方法?
- stm32 - STM32L100C 上的 ADC
- java - 第一个参数类型错误。找到:'com.example.sunshine.FetchData',需要:'android.content.Context'
- flutter - 展开 Stack 小部件内的小部件
- java - Java 可选:映射到子类或超类
- libgdx - 在 libGDX 中的加速度计和 touchDown 之间切换
- javascript - 遍历 N 个输入框,将每个输入框更新为其最大值
- oracle - PLS-00306:调用“ADD_MONTHS”时参数的数量或类型错误
- python - 在 PyQt5 中的 QTreeWidget 和 QListWidget 之间拖动项目?
- laravel - 解密 laravel 会话 cookie 时 unserialize() 出错