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.
Aucun commentaire:
Enregistrer un commentaire