简体   繁体   中英

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. 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'. 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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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