简体   繁体   English

如何在线搜索值并检查它是否符合给定条件

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

I have a file in.txt .我有一个文件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.从这个文件中,我需要搜索number="0x867"并检查它的信息值是否与预期的给定信息值 0x0a 匹配。 if it matches print matches else doesn't matches.如果它匹配 print matches else 不匹配。

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.然后接下来我需要搜索number="0x12"并将其信息值存储,即info="0x0000001A" ,然后搜索number="0x356"并将其信息值info="0x00000234" "0x00000234" 存储到另一个变量。 this value should be equal to previous info value + 0x00000004 (ie 0x0000001A + 0x00000004 = 0x0000001E).该值应等于先前的信息值 + 0x00000004(即 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".如果结果值与info="0x00000234"匹配,则打印number="0x12" info value 0x00000012 + 0x00000004 matches to info value of number="0x356".

else print resulted value not matching否则print resulted value not matching

This is current attempt in python:这是 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.此外,在您的 if 条件下,第一部分是错误的。 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. all() function 将检查行中的子字符串,如果全部存在则返回 true。

Try to convert the.txt file into dataframe and apply the conditions using the pandas datframe尝试将 .txt 文件转换为 dataframe 并使用 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"])

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

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