我有一个对象,看起来像 block = [{'id':'10001', 'date':'2016-01-11', 'text':'this is some text. grab 40'},{'id':'10002', 'date':'2014-03-12', 'text':'this is some more text. grab 60'}]
访问片Python对象中
我想内text
抢项目,并重新格式化我的对象,因此它看起来像: block = [{'id':'10001', 'date':'2016-01-11', 'text':'this is some text. grab 40', 'grabbed': '40'},{'id':'10002', 'date':'2014-03-12', 'text':'this is some more text. grab 60', 'grabbed': '60'}]
我试过
for item in block:
if "grab" in item['text']:
m=re.search('grab (..)',line)
print m
,但得到的错误
Traceback (most recent call last): File "<stdin>", line 3, in <module> File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 146, in search
return _compile(pattern, flags).search(string) TypeError: expected string or buffer
修正了,谢谢。如果我试图抓取的物品的长度不同,并且可能超过两个字符,以至于说'抓(..)'不会总是有效......我怎么能适应这个? – nquestion
所以,如果你想捕捉两个以上的项目,你可以修改你的正则表达式像这样:(。*) - >匹配零个或无字符 - >匹配一个或多个字符 ((+)。 {x}) - >匹配x个字符的实例。 如果你谷歌“正则表达式备忘单”,你会发现有用的指南正则表达式的语法。 – patrick