from mrjob.job import MRJob
import re
Creation_date=re.compile('CreationDate=\"[0-9]*\"[:17]')
class Part2(MRJob):
def mapper(self, _, line):
DateOnly=Creation_date.group(0).split("=")
if(DateOnly > 2013):
yield None, 1
def reducer(self, key, values):
yield key, sum(values)
if __name__ == '__main__':
Part1.run()我已经为MapReduce作业编写了python代码,其中CreationDate="2010-07-28T19:04:21.300“。我必须找到创建日期在2014-01-01或之后的所有日期。但我遇到了一个错误。
发布于 2017-02-16 23:41:24
Regular expression object (re.compile的结果)没有group方法:
>>> pattern = re.compile('CreationDate="([0-9]+)"')
>>> pattern.group
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: '_sre.SRE_Pattern' object has no attribute 'group'要获取match object (具有group方法),您需要使用regex.search method (或根据需要使用regex.match method )将模式与字符串(line)进行匹配:
>>> pattern.search('CreationDate="2013"')
<_sre.SRE_Match object at 0x7fac5c64e8a0>
>>> pattern.search('CreationDate="2013"').group(1) # returns a string
'2013'Creation_date = re.compile('CreationDate="([0-9]+)"')
def mapper(self, _, line):
date_only = Creation_date.search(line), group(1)
if int(date_only) > 2013:
yield None, 1注意:修改了正则表达式,以便将数字部分作为一个组来捕获。并将匹配的字符串转换为int (将字符串与数字2013进行比较没有任何意义,或者根据Python版本引发异常)
发布于 2017-02-16 23:42:21
Creation_date只是一个正则表达式。
您需要匹配输入字符串,然后才能调用group(0)
https://stackoverflow.com/questions/42277921
复制相似问题