Home > Blockchain >  Python - Return substring of string that can change
Python - Return substring of string that can change

Time:12-28

I have a txt file that has a lot of data, and on each line there are 2 pieces of data I need to collect that are seperated by a hyphen.

Example:

Deleted User#0000 - ['000000000003959552', '000000000003959553', '000000000003959554']

I have a function that will be searching the text file for one of the numbers on the RIGHT of the hypen. So if I search for: 000000000003959553 - I want to return: Deleted User#0000 and assign it to a var.

The values will change, but they will always be separated by a hyphen, or any other identifier I want to set as.

What can I do to accomplish this?

CodePudding user response:

We can try parsing the right side of each line as a list, and iterating to see if it contains the value we are searching for. If it does, we return the left side.

from ast import literal_eval

def search_for_value(value):
    for line in file:
        left, right = line.split(' - ')
        for id in literal_eval(right):
            if id == value:
                return left

CodePudding user response:

You can match the regular expression

(?m)^. (?=  -  \[.*'000000000003959553')

Demo.

(?m) sets the multiline flag, causing ^ to match the start of a line, rather than the start of the string (and $ to match the end of line, rather than the end of the string).

^. matches one or more characters at the beginning of a line.

(?= - \[.*'000000000003959553') is a positive lookahead that requires the match to be followed by one or more spaces, a hyphen, one or more space, [, zero or more characters, a single quote, 000000000003959553 and lastly, another single quote.

  • Related