我正在尝试提取关键字(Exhibit)旁边的所有数字匹配项(nn.nn)。例如,through April 25, 2012through April 25, 2012 Exhibit 99.6 Exhibit 99.10这是我的代码。import os,reimport numpy as npos.chdir('C:\\Users\\dul\\Dropbox\\CTO\\test')def extract_data(filename): with open(filename, 'r') as file1: text1=file1.read() matchexh = re.findall(r'Exhibit (\d+).(\d+)',text1) with open('outfile.txt', "a+") as outfile: outfile.write("\n"+matchexh)files= os.listdir("C:\\Users\\dul\\Dropbox\\CTO\\test")for file in files: if ".txt" in file: extract_data(file)当我运行它时,我收到一条错误消息File "C:\Users\dul\Dropbox\CTO\test\exhibitno.py", line 13, in extract_data outfile.write("\n"+matchexh) TypeError: cannot concatenate 'str' and 'list' objects如何获取所有匹配项并列出它们?
1 回答
月关宝盒
TA贡献1772条经验 获得超5个赞
改变这个:
matchexh = re.search(r'Exhibit (\d+).(\d+)',text1).group().strip()
到:
matchexh = re.findall(r'Exhibit (\d+).(\d+)',text1)
添加回答
举报
0/150
提交
取消