简体   繁体   English

搜索文本文件中的字符串,保存包含列表中某项的行,如果文本文件中不存在该项,则返回NA

[英]Search a string in a text file, save a line that contains an item from the list and return NA if the item does not exist in the text file

I have a list of items 我有项目清单

ls=["John","Tanner",'Mary','Anna','25'] 

and a text file: 和一个文本文件:

 V1 'John' 'blablablabla...'
 V2 'Anna' 'blablablabla...'
 V3 'Josh' 'blablablabla...'
 V4 'Mary' 'blablablabla...'
 V5 'Steven' 'blablablabla...'

I want to search each item of the list inside the text file, and save the line that contains the item. 我想搜索文本文件中列表的每个项目,并保存包含该项目的行。

var=[]
with open('C:/Maryam/example/test.txt', 'r') as file:
    for line in file:
        for item in ls:
            if item in line:
                var.append(line)

var   

The output already looks like: 输出已经看起来像:

["  V1 'John' 'blablablabla...'\n","  V2 'Anna' 'blablablabla...'\n",
"  V4 'Mary' 'blablablabla...'\n"]

But I expect to get something like this: 但我希望得到这样的东西:

["  V1 'John' 'blablablabla...'\n", 'NA' , "  V2 'Anna' 'blablablabla...'\n",
"  V4 'Mary' 'blablablabla...'\n", 'NA']

I tried to generate 'NA' in the list but couldn't figure it out. 我试图在列表中生成“ NA”,但无法弄清楚。 The reason I used for line in file: cause I want to save the whole line including specific string. 我在文件中使用行的原因:原因我想保存包括特定字符串在内的整个行。 Is there any way to use regular expressions, so I could get rid of the for loop on the lines and then save the whole line. 有没有办法使用正则表达式,所以我可以摆脱行上的for循环,然后保存整行。 Thank you. 谢谢。

You could change your code to check if anything was found on each different line: 您可以更改代码以检查是否在每个不同的行上都找到了什么:

var=[]
with open('C:/Maryam/example/test.txt', 'r') as file:
    for line in file:
        was_found=False
        for item in var:
            if item in line:
                var.append(line)
                was_found=True
        if not was_found:
            var.append("NA")

I will suggest to use a dictionary instead of a list so you can store an entry for each element to search and the corresponding line (if any, NA otherwise): 我建议使用字典而不是列表,以便为每个要搜索的元素和相应的行存储一个条目(如果有,否则为NA ):

# Initialize the result dictionary
result = {}
for name in ls:
    result[name] = 'NA'

# Process the file
with open('C:/Maryam/example/test.txt', 'r') as file:
    for line in file:
        # For each line check if it contains a reserved keyword
        for name in result.keys():
            if name in line:
                result[name] = line

# Show result (key = name, value = line content)
result

# Show only the values (line contents)
result.values()

The 'for-break-else' construction can be used,too: 可以使用“ for-break-else”构造:

for item in ls:
    if item in line:
        var.append(line)
        break
else:
        var.append("NA")

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

相关问题 尝试将列表中的每个项目保存到文本文件时出现问题 - Issue when trying to save each item in a list to a text file Python 2:在从包含该字符串的文本文件返回整行之前,如何在文本文件中搜索字符串? - Python 2: How do I search for a string in a text file before returning the entire line from the text file that contains this string? 在文本文件中搜索多行字符串并返回 Python 中的行号 - Search a text file for a multi line string and return line number in Python 如何从用户输入中搜索单词列表的文本文件,并打印包含这些单词的行? - how can i search a text file of list of words from user input and print the line which contains these words? 使用 Python 将项目插入文本文件的行中 - Insert Item into line on text file with Python 在包含字符串的文本文件中打印行 - Printing the line in a text file that contains a string 尝试删除从文本文件创建的列表的项目 - Try to remove item for a list created from text file KIVYMD:将项目从文本文件添加到 kivymd 列表 - KIVYMD: adding item to a kivymd list from a text file Python检查列表项是否在文本文件中 - Python check if list item is in text file python在文件中搜索字符串,将整行+下一行返回新文本文件 - python search for string in file return entire line + next line into new text file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM