简体   繁体   中英

Python check if line (two keywords) exists ignoring white spaces between

My example file have a structure:

Library      Collections
Library      XML    use_lxml=True
Library      ute_wtssim
Library      libraries.ta_infomodel
Library      ute_tshark
Library      PDMLChecker.py
Library    libraries.ta_ue_configuration
Library      libraries.ta_tshark
Library    libraries.ta_file_system
Resource     environment_setup.robot
Variables    ../tshark_filters.py
Variables    ../global_variables.py
Variables    tracing_tshark_filters.py

I would like to find in file line: Library libraries.ta_infomodel ignoring whitespaces (column width, which is not constant).

Could you give me advice?

EDIT: I would like to check if line exist... Line which contains exactly two keywords, ignoring white spaces between.

Just use \\s* . \\s stands for a whitespace. * means any number of (including zero):

import re
s = '''Library      Collections
Library      XML    use_lxml=True
Library      ute_wtssim
Library      libraries.ta_infomodel
Library      ute_tshark
Library      PDMLChecker.py
Library      resources.DevWro1.pdml_validation.PdmlValidation
Library    libraries.ta_ue_configuration
Library      libraries.ta_tshark
Library    libraries.ta_file_system
Resource     environment_setup.robot
Variables    ../tshark_filters.py
Variables    ../global_variables.py
Variables    tracing_tshark_filters.py'''

matches = re.findall('Library\s*libraries\.ta_infomodel', s)

for match in matches:
    print(match)

Just try this:

content = """
Library      Collections
Library      XML    use_lxml=True
Library      ute_wtssim
Library      libraries.ta_infomodel
Library      ute_tshark
Library      PDMLChecker.py
Library      resources.DevWro1.pdml_validation.PdmlValidation
Library    libraries.ta_ue_configuration
Library      libraries.ta_tshark
Library    libraries.ta_file_system
Resource     environment_setup.robot
Variables    ../tshark_filters.py
Variables    ../global_variables.py
Variables    tracing_tshark_filters.py
"""

import re
regex = re.compile(r'Library\s+libraries\.ta_infomodel')
line = regex.search(content)

Some explanations. In order to find one line by a regex, you should use re.search , for many re.findall .

\\s+ - for several spaces.

\\. - for the dot. Use have to add the slash because just dot means any character.

Can be done with list comprehension too

import re
lines = open(fname, 'r').readlines()
found = [s for s in lines if re.match(".* Library\s+libraries\.ta_infomodel.*", s)]

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