My need is to get a log build like :
2022-08-22 - something WARN - data1 = 6.3 something| data2 = 7 something | data3 = 8 units
And to at very end have a Json output within
'field1':'2022-08-22','field2':'something','data1':'6.3','data2':'7','data3':'8'
So I need to manage auto naming field1 & field2 keynames & to manage all of "WARN",'-','=','|' as distinct separators
I tried few codes around more or less similar solutions to
split_string = sample_string.split(',|\|=') # just interprets the ',' char only
as first step (current question) to get separators working but I already failed on that part
isn't split the good function/solution to get it using python 2.7 without pandas module (not allowed in my company).
CodePudding user response:
.split
uses its parameter literally. It doesn't look for "one of", it looks for exactly the string you pass. So, do it in steps. First, split(' - ')
. That gives you the date, the pre-WARN string, and a final field with the data.
s = "2022-08-22 - something WARN - data1 = 6.3 something| data2 = 7 something | data3 = 8 units"
p1 = s.split(' - ')
print(p1)
p2 = p1[2].split('|')
print(p2)
data = {}
data['field1'] = p1[0]
data['field2'] = p1[1].strip().split()[0]
for part in p2:
p3 = part.strip().split('=')
data[p3[0]] = p3[1].strip()
print(data)
Output:
['2022-08-22', 'something WARN', 'data1 = 6.3 something| data2 = 7 something | data3 = 8 units']
['data1 = 6.3 something', ' data2 = 7 something ', ' data3 = 8 units']
{'field1': '2022-08-22', 'field2': 'something', 'data1 ': '6.3 something', 'data2 ': '7 something', 'data3 ': '8 units'}