1

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?

7
  • 5
    please show your code Commented Oct 12, 2021 at 14:02
  • use the find method, it will return an index and you can then access the string at that index Commented Oct 12, 2021 at 14:04
  • 3
    this sounds like a great application for regex, and a great tool to help you craft regex search patterns is with regex101.com Commented Oct 12, 2021 at 14:04
  • 1
    "But this code is not efficient." -- why not? Seems reasonable to me. In any event, you might find ast.literal_eval useful for converting the string containing the list into an int list. Commented Oct 12, 2021 at 14:17
  • @JohnColeman Because if you see the Type 2 string then there is app type present like "VTC". So from the Type 2 string I have to extract the type of the app it is and the array also. So I have to have multiple logic for each cases. I want something that will extract the array and if type "VTC" etc are present it will extract that too using a single logic Commented Oct 12, 2021 at 14:20

1 Answer 1

3

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]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.