[英]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.