Souradip Roy
Souradip Roy

Reputation: 300

Extract Values from a string [Python]

I have two type of strings which looks like the following:

#Type 1:
1633659717.630234 DEBUG src/main.rs                    L662  : Binning flow x.x.x.x:xxxx->y.y.y.y:yyyy/UDP: bins (tensors) are [6, 3, 0, 1, 0, 1].
#Type 2: 
1633659717.630234 DEBUG src/main.rs                    L662  : Binning known flow moments for type "VTC": bins (tensors) are [6, 3, 0, 1, 0, 1]

I will like to extract the array at the end of the string and the type if present. I tried doing it using split() function so that the string gets converted to array and I can use indexing to extract the values. But due to uneven presence of white spaces the indexing is not properly working. I also tried doing strip() first but the whitespaces in the array are not getting removed so the split() is also not working properly. I want the array to be a single element after split operation so that indexing can be utilized. Is there any efficient way of doing this? I have put the spaces so that the string looks exactly like what I see. I am able to extract the array but what if there is type present how shall I extract both array and type from the string.

Code:

with open('abc.log','r') as dataFile:
    lines = dataFile.readlines()
    for line in lines:
        if line.__contains__('Binning'):
           print (line.split('are ')[1])

Output:

[6, 3, 0, 1, 0, 1].

But this code is not efficient. I have to have two logic for extracting values. Is there a simpler and easy way?

Upvotes: 1

Views: 627

Answers (1)

j__carlson
j__carlson

Reputation: 1348

You can use re like this:

import ast
import re

ast.literal_eval(re.search("\[.*?\]", my_string).group(0))

Input:

my_string="""
#Type 1:
1633659717.630234 DEBUG src/main.rs                    L662  : Binning flow x.x.x.x:xxxx->y.y.y.y:yyyy/UDP: bins (tensors) are [6, 3, 0, 1, 0, 1]."""

Output:

[6, 3, 0, 1, 0, 1]

Upvotes: 3

Related Questions