tweets = re.findall(r "'text':+.*'truncated'", tweets)
print (tweets)
'text': "RT @premierleague: 🔵 @WayneRooney's chase is on 👀", 'truncated':
我有一个像上面这样的文本字符串,我想检索 'text': 和 'truncated' 之间的推文
。
我已经编写了上面的代码,但收到错误消息
tweets = re.findall(r "'text':+.*'truncated'", tweets)
^
SyntaxError: invalid syntax
我正在使用 findall
,因为推文会重复,并且我想从 findall
搜索中检索所有推文。
谢谢。
最佳答案
无效语法错误是由于r
和正则表达式之间的空格造成的:
tweets = re.findall(r"'text':+.*'truncated'", tweets)
print(tweets)
返回:
['\'text\': "RT @premierleague: \xf0\x9f\x94\xb5 @WayneRooney\'s chase is on \xf0\x9f\x91\x80", \'truncated\'']
仅检索文本:
tweets = re.findall(r"'text':+(.*)'truncated'", tweets)
print(tweets)
返回:
"RT @premierleague: 🔵 @WayneRooney's chase is on 👀",
关于python - 使用正则表达式检索特定文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45648130/