簡體   English   中英

使用來自另一個文件的輸入(帶有扭曲)在文件中搜索文本[Python]

[英]search a file for text using input from another file with a twist [Python]

我想使用queryfile.txt作為源文件,該文件將用於搜索並將每一行匹配到datafile.txt。 但是datafile.txt具有不同的結構。

queryfile.txt應該如下所示:

Gina Cooper

Asthon Smith

Kim Lee

而datafile.txt看起來像這樣:

Gina Cooper

112 Blahblah St., NY

Leigh Walsh

09D blablah, Blah

Asthon Smith

another address here

Kim Lee

another address here

我需要獲取名稱和其后的行。 這是在兩個文件中獲取匹配名稱的代碼,這是dstromberg( https://stackoverflow.com/a/19934477 )的修改代碼:

with open('querfile.txt', 'r') as input_file:
    input_addresses = set(names.rstrip() for names in input_file)

with open('datafile.txt', 'r') as data_file:
    data_addresses = set(names.rstrip() for names in data_file)

with open('names_address.txt', 'w') as output:
    names_address=("\n".join(input_addresses.intersection(data_addresses)))
    output.write(names_address)

總而言之,我想在輸出文件(names_address.txt)中看到的是名稱加上對應於其名稱的地址,這基本上是下一行。 我一個月前才剛開始使用python玩游戲,我相信自己會受困。感謝您的幫助。

改寫這個:

with open('datafile.txt', 'r') as data_file:
    data_addresses = set(names.rstrip() for names in data_file)

對此:

with open('datafile.txt', 'r') as data_file:   
    data = data_file.readlines()
    data_addresses = list(filter(None, [line for line in data if not line[0].isdigit()]))

而是循環瀏覽選項,然后您可以獲取下一個索引:

for i in range(len(data_addresses):
  for entry in input_addresses:
    if entry==data_addresses[i]:
      output.write(data_address[i] + data_address[i+1])

這可能沒有很大的時間復雜度,但是您的數據集出現

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM