Alex A
Alex A

Reputation: 107

How to get a dicitionary key value from a string that contains dictionary?

I have a string that contains dictionary:

data = 'IN.Tags.Share.handleCount({"count":17737,"fCnt":"17K","fCntPlusOne":"17K","url":"www.test.com\\/"});'

How can i get value of an dictionary element count? (In my case 17737)

P.S. maybe I need to delete IN.Tags.Share.handleCount from string before getting a dictionary by i.e. k = data.replace("IN.Tags.Share.handleCount", "") but the problem that '()' remains after delete?

Thanks

Upvotes: 1

Views: 109

Answers (1)

BioGeek
BioGeek

Reputation: 22837

import re, ast
data = 'IN.Tags.Share.handleCount({"count":17737,"fCnt":"17K","fCntPlusOne":"17K","url":"www.test.com\/"});'
m = re.match('.*({.*})', data)
d = ast.literal_eval(m.group(1))
print d['count']

Upvotes: 1

Related Questions