2017-08-31 183 views
0

我是新的Python和我使用pycharm.when我运行此代码在我的python空闲其运行,但是当我在我的pycharm中使用此代码它的显示错误pycharm模块错误NoneType'对象没有属性'组'

我的代码是

import sys 
import re 

for line_string in iter(sys.stdin.readline,''): 
    line = line_string.rstrip() 

    date = re.search(r'date=[0-9]+\-[0-9]+\-[0-9]+', line) 
    date = date.group() 

    print date 

line 8, in <module>
date = date.group()
AttributeError: 'NoneType' object has no attribute 'group'

回答

2

re.search()回报None当它不能匹配的模式。使用它之前,你必须始终检查返回值:

result = re.search(r'date=[0-9]+\-[0-9]+\-[0-9]+', line) 
if result is not None: 
    date = result.group() 
    print date 
else: 
    # Do some error recovery here 
-2
import sys 
import re 

for line_string in iter(sys.stdin.readline,''): 
    line = line_string.rstrip() 

    date = re.search(r'date=[0-9]+\-[0-9]+\-[0-9]+'.decode('utf-8'), line) 
    date = date.group() 

    print date 

工作的方式。该表达式包含非拉丁字符,因此通常会失败。你必须解码为Unicode并使用re.U(Unicode)标志。

+0

@Danial Ahmed同样的错误 – warezers

+0

该表达式不包含任何非拉丁字符。 – DyZ

相关问题