简体   繁体   中英

Python - Find an exact string in a file

i have to find and extract from a text a series of line of these type:

  • featureSetCombination: 1
  • featureSetCombination: 2
  • ...
  • featureSetCombination: 10
  • featureSetCombination: 11
  • ...
  • featureSetCombination: 100

Since only the final number changes in the phrases to search for, my idea is to build the phrase progressively by increasing the final value in this way.

with open('temp.txt') as file:
for line in file:
    num = 1
    str = 'featureSetCombination    : ' + str(num)
    if xxx
        action
        num += 1

The problem is that I have to search for exactly the string with the number; for example the search for "featureSetCombination: 1" would also produce results with "featureSetCombination: 10" or "featureSetCombination: 11" which for what I have to do is not good. I also thought about adding a space after the number to my string, but the idea is not feasible. The only way is by searching for my string exactly. Can you help me? Many thanks:)

you could use regular expression for this, reading the strings an defining the rules for that, in this particular case, either there is a separator or the end of the string so the following code might solve your problem:

import re

# Sample string representing the text to search
string = "featureSetCombination: 1 \n featureSetCombination: 10"

re.findall("featureSetCombination:[1-9][$|\s|.|,|;]", string)
>> ['featureSetCombination:1,']

as you can see it finds the first occurrence but not the second

Have you looked into string method "find"? Here is a tutorial from W3School. It gives useful examples of the syntax for using this method: https://www.w3schools.com/python/ref_string_find.asp

If the sequence is as you listed in your question, the Python "find" method will give you the first result that matches the search criteria. You can end the string with a dot and specify that the end is a dot in the string method to find the exact match. I hope this help!

Alternatively, I would look into Regex for more creative problem-solving solutions.

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