【发布时间】:2012-11-16 20:41:24
【问题描述】:
我正在尝试将来自 Streaming API 的某些值插入到字典中。其中之一是在使用 track=keyword 的过滤方法中使用的术语的值。我已经编写了一些代码,但在打印语句中我收到“遇到异常:'term'”错误。这是我的部分代码:
for term in setTerms:
a = re.compile(term, re.IGNORECASE)
if re.search(a, status.text):
message['term'] = term
else:
pass
print message['text'], message['term']
这是过滤代码:
setTerms = ['BBC','XFactor','Obama']
streamer.filter(track = setTerms)
它匹配字符串,但我还需要能够匹配所有实例,例如。 BBC 还应与#BBC、@BBC 或 BBC1 等匹配。
所以我的问题是如何在 setTerms 中获得一个术语,例如 BBC,以匹配 if re.search(term, status.text) 中的所有这些实例?
谢谢
【问题讨论】:
-
所以简单地说,您只想匹配包含
setTerm列表子字符串的实例?也许只需使用in运算符 stackoverflow.com/questions/3437059/… ?还是我错过了什么?
标签: python regex twitter tweepy