使用正则表达式查找所有匹配项

我正在尝试提取关键字(Exhibit)旁边的所有数字匹配项(nn.nn)。例如,


through April 25, 2012


through April 25, 2012 


Exhibit 99.6 


Exhibit 99.10

这是我的代码。


import os,re

import numpy as np


os.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

如何获取所有匹配项并列出它们?


SMILET
浏览 256回答 1
1回答

月关宝盒

改变这个:matchexh = re.search(r'Exhibit (\d+).(\d+)',text1).group().strip()到:matchexh = re.findall(r'Exhibit (\d+).(\d+)',text1)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python