Parsing in python

I have a line

Adid: 4028cb901dd9720a011e1160afbc01a3; siteId: 8a8ee4f720e6beb70120e6d8e08b0002; User ID: 5082a05c-015e-4266-9874-5dc6262da3e0

I need only the value of adId, siteId and userId. means

4028cb901dd9720a011e1160afbc01a3

8a8ee4f720e6beb70120e6d8e08b0002

5082a05c-015e-4266-9874-5dc6262da3e0

all 3 in different variables or in an array so that I can use all three

+3
source share
4 answers

You can do something like this:

input='adId:4028cb901dd9720a011e1160afbc01a3;siteId:8a8ee4f720e6beb70120e6d8e08b0002;userId:5082a05c-015e-4266-9874-5dc6262da3e0'

result={}
for pair in input.split(';'):
    (key,value) = pair.split(':')
    result[key] = value

print result['adId']
print result['siteId']
print result['userId']
+1
source

You can break them down into a dictionary if you don't need any parsing:

In [2]: dict(kvpair.split(':') for kvpair in s.split(';'))
Out[2]:
{'adId': '4028cb901dd9720a011e1160afbc01a3',
 'siteId': '8a8ee4f720e6beb70120e6d8e08b0002',
 'userId': '5082a05c-015e-4266-9874-5dc6262da3e0'}
+18
source
matches = re.findall("([a-z0-9A-Z_]+):([a-zA-Z0-9\-]+);", buf)

for m in matches:
    #m[1] is adid and things
    #m[2] is the long string.

{32},

([a-zA-Z0-9]+){32};

.

+1

There is a great method called split () for python that will work well for you. I would suggest using it twice, once for ';' then again for each of those using ":".

0
source

Source: https://habr.com/ru/post/1709377/


All Articles