![](/img/trans.png)
[英]How to get value from one column of a text file for values of another column
[英]Read each value one by one in a column with respect to another values in another column from a text file
例如,我有一个这样的文本文件。 共有三列。
data 5 0.1
data 4 0.2
data 2 0.3
data 1 0.5
我有的代码:
for line in open("myfile.txt", "r").readlines():
line = line.split()
if len(line)>1 and line[0] == 'data':
time = line[1]
volume = line[2]
volume_per_time=float(volume)/float(time)
print(volume_per_time)
我想在 while 循环中打印 volume_per_time。 即我想相对于第二列第一值调用第三列第一值。 然后一一加上循环功能。
您可以将计算出的值插入数组并将其用于绘图。
volume_per_time = []
for line in open("myfile.txt", "r").readlines():
line = line.split()
if len(line)>1 and line[0] == 'data':
time = line[1]
volume = line[2]
volume_per_time.append(float(volume)/float(time))
print(volume_per_time)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.