简体   繁体   English

python中的re.search字符串

[英]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. 您不能将re.search直接应用于list或tuples或dict,它应该接受string作为最后一个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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