Julian Silvestri
Julian Silvestri

Reputation: 2037

Extract ID SET from dynamic string

I am struggling trying to figure this out.
I am not very good with manipulating a dynamic string set.
I have a dynamic text file with thousands of lines.
Each line begins with a number (text file is not ordered) then contains an id (which may or may not contain numbers) and ends with data string which can also contain numbers.

Example would be:

135: IDDataHere:DataString

The data sets range anywhere from numbers spanning above 7000 and as small as 1. Another example of a data set is:

7124: Id124WithNum2 :DataStringsWIthnum1231

or

1: ID12Nums :DataString231

or

12: IDWithNum1 :DataStrings

I am trying to extract the ID data set in between the two colons ':' Below is what I have tried

for lines in text_file:
    if user_input in lines:
        #print(lines.replace(" ", ""))
        if len(lines) > stop :
            lines = re.sub('{0}:'.format(range(0,8000)), '', lines)
            print(lines)
    else:
        continue

Ive tried something like this using re but no luck.
How can I extract the id into a new variable from a dynamic string file where the strings never match and contain numbers inside the id?

Upvotes: 0

Views: 57

Answers (1)

Red
Red

Reputation: 27577

I am trying to extract the ID data set in between the two colons ':'

Simply use the split() method:

string.split(':')[1]

for lines in text_file:
    if user_input in lines:
        if len(lines) > stop :
            print(lines.split(':')[1])
    else:
        continue

Upvotes: 1

Related Questions