Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个文本文档,我想从中提取某些短语。该短语是“MmarC5_”,后跟 4 个数字。这是我到目前为止所拥有的:
with open("file.txt") as f: re = (MmarC5_) re.findall(MmarC5_\d{4}", f.read())
我不断收到错误:
NameError: name 'MmarC5' is not defined.
你忘记了引号:
re.findall(r"MmarC5_\d{4}", f.read())
而且这一行没有任何意义,删除它:
re = (MmarC5_)
你导入re模块了吗?
re
import re
我相信你想为正则表达式指定 r 并将正则表达式括在单引号中,所以它应该看起来像......
re.findall(r'MmarC5_\d{4}', f.read())