简体   繁体   中英

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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM