Home > Enterprise >  Python List Extract data from a comma separated list
Python List Extract data from a comma separated list

Time:11-28

Im looking for best way to extract all the Count.AutoSlam.OAK4.3. [Weekly Avg: 0.56] from this list . I mainly need to know the 3 and the 0.56 positions. Then place them into seprate list for pandas.

['Label,Min,Avg,Max,Nov 23,11:00,Nov 23,12:00,Nov 23,13:00,Nov 23,14:00,Nov 23,15:00,Nov 23,16:00,Nov 23,17:00,Nov 23,18:00,Nov 23,19:00,Nov 23,20:00,Nov 23,21:00,Nov 23,22:00,Nov 23,23:00,Nov 24,00:00,Nov 24,01:00,Nov 24,02:00,Nov 24,03:00,Nov 24,04:00,Nov 24,05:00,Nov 24,06:00,Nov 24,07:00,Nov 24,08:00,Nov 24,09:00,Nov 24,10:00,Nov 24,11:00,Nov 24,12:00,Nov 24,13:00,Nov 24,14:00,Nov 24,15:00,Nov 24,16:00,Nov 24,17:00,Nov 24,18:00,Nov 24,19:00,Nov 24,20:00,Nov 24,21:00,Nov 24,22:00,Nov 24,23:00,Nov 25,00:00,Nov 25,01:00,Nov 25,02:00,Nov 25,03:00,Nov 25,04:00,Nov 25,05:00,Nov 25,06:00,Nov 25,07:00,Nov 25,08:00,Nov 25,09:00,2 - Count.AutoSlam.OAK4.3. [Weekly Avg: 0.56] 0.0 0.56 2.38 0.0 2.27 0.0 0.0 0.16 0.30 0.25 1.07 1.79 2.38 0.0 0.98 0.0 0.0 0.0 0.0 0.41 0.47 0.60,7 - Count.AutoSlam.OAK4.18 [Weekly Avg: 0.29] 0.0 0.29 2.34 0.0 0.0 0.0 0.0 0.0 0.0 2.34 0.0,5 - Count.AutoSlam.OAK4.13 [Weekly Avg: 0.34] 0.0 0.34 2.08 0.83 0.30 0.32 0.19 0.26 0.47 0.36 0.0 0.22 0.11 0.36 0.65 0.41 0.52 0.85 0.88 1.28 0.0 0.0 1.19 0.0 0.0 0.0 0.0 0.0 2.08 0.0 0.0 0.45 0.79 0.32 0.0 0.0 0.0 0.0 0.0 0.35 0.0 0.15,1 - Count.AutoSlam.OAK4.6. [Weekly Avg: 0.59] 0.0 0.59 1.79 0.0 0.34 0.11 0.38 0.24 0.19 0.15 0.42 0.69 0.56 0.26 1.26 0.71 1.79 1.51 0.82 1.10 1.40 0.57 0.0 0.85 0.34 0.0 0.15 0.29 0.86 0.35 0.39 0.78 1.09 1.35 0.68 0.70 1.02 1.66 1.15 0.31 0.0 0.0 0.0 0.077 0.13,11 - Count.AutoSlam.OAK4.19 [Weekly Avg: 0.23]']

CodePudding user response:

You could try using a regex to get matching groups and then just get the groups, e.g.

/Count\.AutoSlam\.OAK4\.(?P<autoslam>\d ) \[Weekly Avg: (?P<weekly_avrg>\d\.\d{2})\]/

See Example on Regex101.

Now, you can capture the data in python with:

import re

pattern = re.compile(r"Count\.AutoSlam\.OAK4\.(?P<autoslam>\d ) \[Weekly Avg: (?P<weekly_avrg>\d\.\d{2})\]")

# find all matches to groups
for match in pattern.finditer(string_list[0]):
    print(match.group('autoslam'))
    print(match.group('weekly_avrg'))

Find details on capturing groups for instance on pynative.com. If your list contains multiple strings like that you either need to merge (join) them or process them individually...

  • Related