简体   繁体   中英

re.search string in python

I want to take screen name out of strings like this:

text = (u'675739242841120768', [{u'id': 9207632, u'indices': [3, 15], u'id_str': u'9207632', u'screen_name': u'brainpicker', u'name': u'Maria Popova'}])

I'm not getting any results with this code I thought I needed to use:

re.search("screen_name': u'(...)', u'name", text)

I'm not sure if I am suppose to add something else inside the parenthesis.

You need to specify the exact index value since the variable text contain combination of tuples, list, dictionaries.

>>> text = (u'675739242841120768', [{u'id': 9207632, u'indices': [3, 15], u'id_str': u'9207632', u'screen_name': u'brainpicker', u'name': u'Maria Popova'}])
>>> text[1][0]['screen_name']
u'brainpicker'

You can't apply re.search directly to list or tuples or dict , it should accept string as last parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM