[英]Read each value one by one in a column with respect to another values in another column from a text file
For example, I have a text file like this.例如,我有一个这样的文本文件。 There are three columns.共有三列。
data 5 0.1
data 4 0.2
data 2 0.3
data 1 0.5
Codes I have:我有的代码:
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)
I want to print the volume_per_time in a while loop.我想在 while 循环中打印 volume_per_time。 ie I want to call the 3nd column 1st value with respect to 2nd column 1st value.即我想相对于第二列第一值调用第三列第一值。 And then one by one with loop function.然后一一加上循环功能。
you can insert the calculated value to an array and use it for the plotting.您可以将计算出的值插入数组并将其用于绘图。
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.