简体   繁体   English

相对于文本文件中另一列中的另一个值,逐个读取列中的每个值

[英]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.

相关问题 如何从文本文件的一列获取值以获取另一列的值 - How to get value from one column of a text file for values of another column 用熊猫中另一列的每个值替换一个列的值 - Replace values of one column for each value of another column in pandas 仅使用标准库,按另一列的分组值中的一列的累积总数对文本文件进行排序? - Sorting a text file by cumulative total of one column from grouped values in another column using standard library only? Python pandas 用模式(同一列 -A)相对于 Pandas 数据帧中的另一列替换一列(A)的 NaN 值 - Python pandas replace NaN values of one column(A) by mode (of same column -A) with respect to another column in pandas dataframe 通过两个文件中的一列中的值与另一列中的相应值进行汇总 - Aggregating values in one column by their corresponding value in another from two files 从一列中删除等于另一列中的值的值 - Remove values from one column, that are equal to the value in another 一列的值出现在另一列中 read_csv Pandas - Values of one column appears in another column with read_csv Pandas 根据另一个文件中的数据计算一列的值 - Calculate values of a column of one based on data from another file Pandas,对于一列中的每个唯一值,在另一列中获取唯一值 - Pandas, for each unique value in one column, get unique values in another column Python Pandas - 过滤 pandas dataframe 以获取一列中具有最小值的行,以获取另一列中的每个唯一值 - Python Pandas - filter pandas dataframe to get rows with minimum values in one column for each unique value in another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM