简体   繁体   中英

How to search for a value in line and check if it matches given condition

I have a file in.txt .


name="XYZ_PP_0" number="0x12" bytesize="4" info="0x0000001A"
name="GK_LMP_2_0" number="0xA5" bytesize="8" info="0x00000000bbae321f"
name="MP_LKO_1_0" number="0x356" bytesize="4" info="0x00000234"
name="PNP_VXU_1_2_0" number="0x48A" bytesize="8" info="0x00000000a18c3ba3"
name="AVU_W_2_3_1" number="0x867" bytesize="1" info="0x0b"

From this file i need to search for number="0x867" and check if it's info value matches to the expected given info value which is 0x0a. if it matches print matches else doesn't matches.

then next i need to search for number="0x12" and store it's info value ie info="0x0000001A" and then search for number="0x356" and store it's info value info="0x00000234" to another variable. this value should be equal to previous info value + 0x00000004 (ie 0x0000001A + 0x00000004 = 0x0000001E).

if resulted value matches to info="0x00000234" then print number="0x12" info value 0x00000012 + 0x00000004 matches to info value of number="0x356".

else print resulted value not matching

This is current attempt in python:

with open("in.txt", "r") as infile:

     XYZ = False
     MP = False
     AVU = False
 
     xyz = ['number="0x12"', 'info="0x0000001A"']
     mp  = ['number="0x356"', 'info="0x00000234"']
     avu = ['number="0x867"', 'info="0x0b"']

     for line in infile:
         if all(x in line for x in xyz):
            XYZ = True
            continue

         if all(x in line for x in mp):
            MP = True
            continue
 
         if all(x in line for x in avu):
            AVU = True
            continue  



       

but this code will simply checks if the line is present in file or not. it won't check the conditions mentioned above.

Is there a way i can search for the number in the text file and store it's info value to variable?

You have to escape the double quotes, or you can use single quotes so that you string can have double quotes in it like this:

'number="0x12"'

Also, in your if condition the first part is wrong. Here is the loop:

xyz_list = ['number="0x12"', 'info="0x0000001A"']
mp_list  = ['number="0x356"', 'info="0x00000234"']
avu_list = ['number="0x867"', 'info="0x0b"']

for line in infile:
     if all(x in line for x in xyz_list):
         XYZ = True
         continue

     if all(x in line for x in mp_list):
         MP = True
         continue
 
     if all(x in line for x in avu_list):
         AVU = True
         continue   

The all() function will check substrings in the line and returns true if all exist.

Try to convert the.txt file into dataframe and apply the conditions using the pandas datframe

import pandas as pd
df = pd.read_fwf('myfile.txt')
or 
df = pd.read_csv('myfile.txt', sep=" ")
or 
df = pd.read_csv('myfile.txt', ,delimiter="\t")
or 
df = pd.read_csv('myfile.txt', sep=" ", header=None, names=["Pos", "Lang", "Perc"])

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