-1

I am trying to extract all matches of numbers (nn.nn) next to a keyword (Exhibit). For example,

through April 25, 2012

through April 25, 2012 

Exhibit 99.6 

Exhibit 99.10

Here is my code.

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)

When I run this, I get an error message

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

How can I get all matches and list them?

TrebledJ
  • 8,713
  • 7
  • 26
  • 48
Andre
  • 23
  • 4

1 Answers1

1

Change this:

matchexh = re.search(r'Exhibit (\d+).(\d+)',text1).group().strip()

to:

matchexh = re.findall(r'Exhibit (\d+).(\d+)',text1)
hacker315
  • 1,996
  • 2
  • 13
  • 23