HI do you know how to split like this:
string: [20221110.112127.316856][info]:[PDCP DL- ingress traffic: 0.000000(Mbps), egress traffic: 0.000000(Mbps)]
How can I split like [ ‘ingress traffic:’ , ‘0.000000(Mbps)’, ‘egress traffic:’, ‘0.000000(Mbps)’]
If I were doing this, I’d be inclined to match more rigidly. For
example, instead of just grabbing everything which is not an opening
bracket, grab the labels (eg “ingress traffic”), the delimiter (": ")
and the numeric parts (“0.000000”) specificly. Maybe even the units
inside the brackets. This avoids mismatching incorrectly/inappropriate
lines, and also makes it easier to deal with the matched parts.
Example (untested):
(ingress traffic):\S+(\d+\.\d+)
This (a) matches your label specificly and (b) matches a number. It also
puts groups (the brackets) around the label and number; those groups are
then available in the match object directly (in your code, the searchtest variable - the result of the re.search call).
Then you can for example use the label as a key in a dict and convert
the numeric string to an actual number, eg:
label = searchtest.group(0)
numeric = searchtest.group(1)
number = float(numeric)
Ahh… I’m an idiot. I mean \s (whitespace), not \S (nonwhitespace).
Try a lowercase s in the above regexp.
When you have that working, you can do things like a loop for the
various labels (to use the same code), or re.findall() with a pattern to
match any label, then look up the labels it finds (or just build a dict with keys from all the labels).
Well nothing says you need to put things into a dict, you can just append the label and the numeric part onto your list of results. And you can make the pattern for the numeric part include the bracketed unit (Mbps) as desired.