繁体   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