根据Python中的值查找第一行文本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了根据Python中的值查找第一行文本相关的知识,希望对你有一定的参考价值。

如何在Python的“file.txt”列表中搜索第一个“纬度,经度”坐标的值,并获得上面3行和下面3行?

37.0459

file.txt的

37.04278,-95.58895
37.04369,-95.58592
37.04369,-95.58582
37.04376,-95.58557
37.04376,-95.58546
37.04415,-95.58429
37.0443,-95.5839
37.04446,-95.58346
37.04461,-95.58305
37.04502,-95.58204
37.04516,-95.58184
37.04572,-95.58139
37.04597,-95.58127
37.04565,-95.58073
37.04546,-95.58033
37.04516,-95.57948
37.04508,-95.57914
37.04494,-95.57842
37.04483,-95.5771
37.0448,-95.57674
37.04474,-95.57606
37.04467,-95.57534
37.04462,-95.57474
37.04458,-95.57396
37.04454,-95.57274
37.04452,-95.57233
37.04453,-95.5722
37.0445,-95.57164
37.04448,-95.57122
37.04444,-95.57054
37.04432,-95.56845
37.04432,-95.56834
37.04424,-95.5668
37.044,-95.56251
37.04396,-95.5618

预期结果

37.04502,-95.58204
37.04516,-95.58184
37.04572,-95.58139
37.04597,-95.58127
37.04565,-95.58073
37.04546,-95.58033
37.04516,-95.57948

附加信息

在linux中,我可以使用grep,sed,cut等来获得最接近的行并进行我需要的处理,但我喜欢Python。

任何帮助将不胜感激!谢谢。

答案

如何在Python的“file.txt”列表中搜索第一个“纬度,经度”坐标的值,并获得3行以上3行?*


你可以试试:

with open("text_filter.txt") as f:
    text = f.readlines() # read text lines to list

    filter= "37.0459"
    match = [i for i,x in enumerate(text) if filter in x] # get list index of item matching filter
    if match:
        if len(text) >= match[0]+3: # if list has 3 items after filter, print it
            print("".join(text[match[0]:match[0]+3]).strip())
        print(text[match[0]].strip())
        if match[0] >= 3:  # if list has 3 items before filter, print it
            print("".join(text[match[0]-3:match[0]]).strip())

输出:

37.04597,-95.58127
37.04565,-95.58073
37.04546,-95.58033
37.04597,-95.58127
37.04502,-95.58204
37.04516,-95.58184
37.04572,-95.58139
另一答案

您可以使用pandas导入数据框中的数据,然后轻松地对其进行操作。根据您的问题,要检查的值不是完全匹配,因此我将其转换为字符串。

import pandas as pd
data = pd.read_csv("file.txt", header=None, names=["latitude","longitude"]) #imports text file as dataframe
value_to_check = 37.0459 # user defined
for i in range(len(data)):
    if str(value_to_check) == str(data.iloc[i,0])[:len(str(value_to_check))]:
        break
print(data.iloc[i-3:i+4,:])

产量

    latitude  longitude
9   37.04502  -95.58204
10  37.04516  -95.58184
11  37.04572  -95.58139
12  37.04597  -95.58127
13  37.04565  -95.58073
14  37.04546  -95.58033
15  37.04516  -95.57948
另一答案

一个带迭代器的解决方案,它只在内存中保留必要的行,并且不加载文件中不必要的部分:

from collections import deque
from itertools import islice


def find_in_file(file, target, before=3, after=3):

    queue = deque(maxlen=before)
    with open(file) as f:
        for line in f:
            if target in map(float, line.split(',')):
                out = list(queue) + [line] + list(islice(f, 3))
                return out
            queue.append(line)
        else:
            raise ValueError('target not found')

一些测试:

print(find_in_file('test.txt', 37.04597))

# ['37.04502,-95.58204
', '37.04516,-95.58184
', '37.04572,-95.58139
', '37.04597,-95.58127
',
#  '37.04565,-95.58073
', '37.04565,-95.58073
', '37.04565,-95.58073
']

print(find_in_file('test.txt', 37.044))  # Only one line after the match

# ['37.04432,-95.56845
', '37.04432,-95.56834
', '37.04424,-95.5668
', '37.044,-95.56251
', 
#   '37.04396,-95.5618
']

此外,如果在匹配之前或之后的行数少于预期的数量,它也可以工作。我们匹配浮点数,而不是字符串,因为'37 .04'会错误地匹配'37 .0444'否则。

另一答案

这个解决方案将打印前后元素,即使它们小于3.我也使用字符串,因为它也是你想要部分匹配的问题所暗示的。即。 37.0459将匹配37.04597

search_term='37.04462'
with open('file.txt') as f:
    lines = f.readlines()
lines = [line.strip().split(',') for line in lines] #remove '
'
for lat,lon in lines:
    if search_term in lat:
        index=lines.index([lat,lon])
        break
left=0
right=0
for k in range (1,4): #bcoz last one is not included
    if index-k >=0:
        left+=1
    if index+k<=(len(lines)-1):
        right+=1
for i in range(index-left,index+right+1): #bcoz last one is not included
    print(lines[i][0],lines[i][1])

以上是关于根据Python中的值查找第一行文本的主要内容,如果未能解决你的问题,请参考以下文章

python2根据索引表查找相应的录音片段拼接录音

excel中如何在几中列查找并返回第一列

如何根据Python中另一列中的日期查找最频繁的值

Excel根据标题比较两列以查找同一行中的匹配或差异

如何在 Excel VBA 中为目标查找同一行的第一个单元格的值?

grep命令保留第一行(C语言实现)