我尝试提取以下单词“good”后面的单词:
lu = ["Don't waste your time with season two. She could be fun if she wasn't so hateful inside. Not good.* Yuck.",
"I only watched one episode. It had too much nudity and sex in it. The story line was good but didn't enjoy the rest."]
代码是:
import re
lu = ["Don't waste your time with season two. She could be fun if she wasn't so hateful inside. Not good.* Yuck.",
"I only watched one episode. It had too much nudity and sex in it. The story line was good but didn't enjoy the rest."]
rx = re.compile(r'good\s+(\w+)')
user = tuple(map(lambda x: x.group(1) or "", map(rx.search, f)))
print(user)
但我得到的输出是:
AttributeError: 'NoneType' object has no attribute 'group'
我希望获得的输出是:
('Yuck', 'but')
或:
(['Yuck'], ['but'])
当我从 lu
中删除点 (.) 时,程序运行没有错误,但我不想手动删除点 (.)。谁能帮帮我
答案 0 :(得分:0)
问题在于您的正则表达式 is not matching the first string。
要处理这种情况,您需要检查 x
是否为 None:
rx = re.compile(r'good\s+(\w+)')
user = tuple(map(lambda x: "" if x is None else x.group(1), map(rx.search, lu)))
print(user)
# Outputs ('', 'but')
修正正则表达式:
rx = re.compile(r'good[^\w]+(\w+)')
user = tuple(map(lambda x: "" if x is None else x.group(1), map(rx.search, lu)))
print(user)
# Outputs ('Yuck', 'but')