【发布时间】:2016-12-06 20:14:16
【问题描述】:
我有一个类似的文件
2.0 4 3
0.5 5 4
-0.5 6 1
-2.0 7 7
.......
实际文件很大
我想阅读并添加几列,第一列,column(4) = column(2) * column(3),第二列添加将是column 5 = column(2)/column(1) + column(4),所以结果应该是
2.0 4 3 12 14
0.5 5 4 20 30
-0.5 6 1 6 -6
-2.0 7 7 49 45.5
.....
我想写在不同的文件中。
with open('test3.txt', encoding ='latin1') as rf:
with open('test4.txt', 'w') as wf:
for line in rf:
float_list= [float(i) for i in line.split()]
print(float_list)
但到目前为止,我只有这个。我只能创建列表,但不确定如何在列表上执行算术并创建新列。我想我完全不在这儿了。我只是python的初学者。任何帮助将不胜感激。谢谢!
【问题讨论】:
-
您在行尾有新值,因此将新值附加到列表
float_list.append()。稍后使用' '.join()生成行。或者使用csv模块,空格作为分隔符。 -
@juanpa.arrivillaga:你建议我怎么做?我对 python 以及一般编程都很陌生。如果你能具体一点,那将非常有帮助。谢谢。
标签: python-3.x file-io