简体   繁体   English

在读取文本文件时提取列值

[英]extracting column values while reading the text file

i have a file saved in text that contain number of rows.I want to read row by row and trying to extract second and third column value at each iteration and want to do some processing with them.(for example while reading first row of the below text file i need to extract value 0.1115 and 0.2 for calculation..我有一个保存在包含行数的文本中的文件。我想逐行读取并尝试在每次迭代时提取第二列和第三列值,并希望对它们进行一些处理。(例如,在读取第一行时在文本文件下方,我需要提取值 0.1115 和 0.2 进行计算..

the data i have in the below format and saved in test.txt我拥有以下格式的数据并保存在 test.txt 中

/home/sio/testfile 0.1115  0.2  0.8
/home/sio/testfile 0.50  0.4  0.1
/home/sio/testfile 0.9  0.7  0.7
/home/sio/testfile 0.4  0.8  0.4
/home/sio/testfile 0.7  0.9  2.3

i tried the code below but it gives me error:我尝试了下面的代码,但它给了我错误:

import numpy as np

a = open("test.txt","r")
b = a.readlines()
a.close()
  while readline(b)
        secondcolumn=
        thirdcolumn=
    
import numpy as np

a = open("test.txt","r")
b = a.readlines()
a.close()
for line in b:
    columns=line.split()
    if len(columns)>=2:
        col2=columns[1]
        col3=columns[2]
        print(col2,col3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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