2017-09-15 63 views
-1

所以我试图用一个“column_matches”函数来搜索带有数据的txt文件,该数据是存储在数组中的,用于列中特定值然后打印包含该值的行。在制表符分隔的数组中搜索一列以获取特定值

我的代码现在看起来是这样的:

f = open(r'file_directory') 
a = [] 
for line in f: 
    a.append(line) 

def column_matches(line, substring, which_column): 
     for line in a: 
      if column_matches(line, '4', 6): 
       print (line) 
      else: 
       print('low multiplicity') 

在这个例子中,我试图寻找第7列的值4。然而,这是目前没有任何打印。 我是一名初学者程序员,所以这可能是非常错误的,但会喜欢一些反馈,因为我无法从其他人的问题中解决它。 理想情况下,程序应搜索所有行并在特定列中打印(或保存)每行具有特定值的行!

编辑:例如输入:

K00889.01 0.9990 8.884922995 10.51 0.114124 89.89 1 153 0.8430 0.8210

K01009.01 0.0000 5.09246539 1.17 0.014236 89.14 1 225 0.7510 0.7270

+1

你不是在调用函数'column_matches()',除了它本身。我不确定'column_matches'应该如何工作,因为函数中包含的唯一逻辑依赖于它自己的输出。 – roganjosh

+0

在完成构建列表之后,在定义函数之前,还有'print(a [0] [6])'',print(type(a [0] [6]))'的输出是什么?你调用第二个参数“substring”,然后提供一个整数,所以我不确定你想要匹配什么,你没有提供任何示例输入。 – roganjosh

+0

@roganjosh ,所以是的,我猜它应该是'4'。打印(a [0] [6])由于某种原因不会返回任何内容。 –

回答

0

你的现有功能没有按” t实际上有任何逻辑来处理您要搜索的案例。事实上,你有if column_matches(line, '4', 6):里面函数column_matches所以你暗示,它必须调用自己,以确定采取什么行动......逻辑上只是形成一个无限循环(虽然在你的情况,实际上没有运行)。

这应该是类似于您现有的方法,但应该做你想做的。它应该对你的实际文件结构相对有弹性,但是让我知道它是否会引发错误。

data = [] 
with open('example.txt', 'r') as infile: 
    # Will automatically close the file once you're done reading it 
    for row in infile: 
     data.append(row.replace('\n', '').split()) 


def column_matches(line, target, column_index): 
    try: 
     file_data = int(line[column_index]) 
     if file_data == target: 
      return True 
     else: 
      return False 
    except ValueError: 
     print('Not a valid number: {}'.format(line[column_index])) 
     return False 

matching_rows = [] # To store items in data that meet our criteria 
for line in data: 
    if column_matches(line, 4, 6): 
     matching_rows.append(line) # Function has to return True for this to happen 
+0

这工作完美!并且容易遵循。不胜感激! –

相关问题