简体   繁体   English

使用python在文本中搜索字符串

[英]Search a string in a text using python

I have a list 我有一份清单

list=["John","Tanner",'Mary','Anna',"Oscar",'ID'] 

and a text file. 和一个文本文件。 Inside the text file looks like the following: 文本文件内部如下所示:

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

I want to search the list inside the text file, and at the end I want to get the results as: 我想搜索文本文件中的列表,最后我想得到的结果如下:

RESULT=[1,'NA',4,2,'NA',0].

1,4 and 2 are the numbers extracted from V1, V4 and V2. 1,4和2是从V1,V4和V2中提取的数字。 Here is the code: 这是代码:

start_time = time.time()
for item in list:
with open('C:/Mary/test.txt', 'r') as file:
    for line in file:
        if item in line:
            var_name=line.split()[0]
            if var_name=='ID':
                var_loc.append(0)
            else:
                var_loc.append(int(re.split('(\d+)',var_name)[1]))
            break
        #else:
         #   var_loc.append('NA')

total_time = time.time() - start_time
total_time

Question: When I pick an item from the list and search inside the text, and then it does not exist in the text, I want it to return 'NA'. 问题:当我从列表中选择一个项目并在文本中搜索,然后它在文本中不存在时,我希望它返回'NA'。 But I couldn't figure it out to do it correctly. 但我无法弄清楚这是否正确。 As I said I want the result looks like this at the end: 正如我所说,我希望结果在最后看起来像这样:

RESULT=[1,'NA',4,2,'NA',0].

Thanks. 谢谢。

You can do it using regular expressions: 您可以使用正则表达式执行此操作:

with open('C:/Mary/test.txt', 'r') as f:
    s = f.read()
    for item in items:
        if item == 'ID':
            var_loc.append(0)
            continue

        m = re.search(f"^V(\d+) '{item}'", s, flags=re.MULTILINE)
        if m is None:
            var_loc.append('NA')
        else:
            var_loc.append(m.group(1))

Please, don't use list keyword as variable name - it's a keyword for python's list function (I replaced it with items ). 请不要使用list关键字作为变量名-它是Python的关键字list功能(我与取代它items )。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM