如何在目录中搜索所有文件类型以获取正则表达式

问题描述 投票:3回答:2

所以,我想在整个目录中搜索包含正则表达式列表的文件。其中包括:目录,pdfs和csv文件。在仅搜索文本文件时,我可以成功完成此任务,但搜索所有文件类型是一种困难。以下是我到目前为止的工作:

import glob
import re
import PyPDF2
#-------------------------------------------------Input----------------------------------------------------------------------------------------------
folder_path = "/home/"
file_pattern = "/*"
folder_contents = glob.glob(folder_path + file_pattern)


#Search for Emails
regex1= re.compile(r'\S+@\S+')
#Search for Phone Numbers
regex2 = re.compile(r'\d\d\d[-]\d\d\d[-]\d\d\d\d')
#Search for Locations
regex3 =re.compile("([A-Z]\w+), ([A-Z]{2})")


for file in folder_contents:
    read_file = open(file, 'rt').read()
if readile_file == pdf:

    pdfFileObj = open('pdf.pdf', 'rb') 

    pdfReader = PyPDF2.PdfFileReader(pdfFileObj) 

    pageObj = pdfReader.getPage(0)  

    content= pageObj.extractText()) 

    if regex1.findall(read_file) or regex2.findall(read_file) or regex3.findall(read_file):
        print ("YES, This file containts PHI")
        print(file)
    else:
        print("No, This file DOES NOT contain PHI")
        print(file)

当我运行这个我得到这个错误:

YES, This file containts PHI
/home/e136320/sample.txt
No, This file DOES NOT contain PHI
/home/e136320/medicalSample.txt

---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-129-be0b68229c20> in <module>()
     19 
     20 for file in folder_contents:
---> 21     read_file = open(file, 'rt').read()
     22 if readile_file == pdf:
     23     # creating a pdf file object
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-128-1537605cf636> in <module>()
     18 
     19 for file in folder_contents:
---> 20     read_file = open(file, 'rt').read()
     21     if regex1.findall(read_file) or regex2.findall(read_file) or regex3.findall(read_file):
     22         print ("YES, This file containts PHI")

/jupyterhub_env/lib/python3.5/codecs.py in decode(self, input, final)
    319         # decode input (taking the buffer into account)
    320         data = self.buffer + input
--> 321         (result, consumed) = self._buffer_decode(data, self.errors, final)
    322         # keep undecoded input until the next call
    323         self.buffer = data[consumed:]

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7 in position 10: invalid continuation byte

有什么建议?

python regex pypdf2 os.path
2个回答
1
投票

你无法打开这样的pdf文件,它期待一个纯文本文件。你可以使用这样的东西:

fn, ext = os.path.splitext(file)

if ext == '.pdf':
    open_function = PyPDF2.PdfFileReader
else:  # plain text
    open_function = open

with open_function(file, 'rt') as open_file:
    # Do something with open file...

这个片段检查文件扩展名,然后根据它找到的内容分配一个打开的函数,这有点天真,可以用类似于in this answer的方法更好地完成。


0
投票

您以相同的方式打开不同的文件类型时出现问题。你必须把它们整理出来。 CSV可以直接读取,但pdf不能这样做。我使用re.search(r".*(?=pdf$)",file)来防止2.pdf.csv被认为是一个pdf文件

import glob
import re
import PyPDF2
#-------------------------------------------------Input----------------------------------------------------------------------------------------------
folder_path = "/home/e136320/"
file_pattern = "/*"
folder_contents = glob.glob(folder_path + file_pattern)

#Search for Emails
regex1= re.compile(r'\S+@\S+')
#Search for Phone Numbers
regex2 = re.compile(r'\d\d\d[-]\d\d\d[-]\d\d\d\d')
#Search for Locations
regex3 =re.compile("([A-Z]\w+), ([A-Z]{2})")


for file in folder_contents:

    if re.search(r".*(?=pdf$)",file):
        #this is pdf
        with open(file, 'rb') as pdfFileObj:
            pdfReader = PyPDF2.PdfFileReader(pdfFileObj) 
            pageObj = pdfReader.getPage(0)  
            content = pageObj.extractText()
            read_file = #

    elif re.search(r".*(?=csv$)",file):
        #this is csv
        with open(file,"r+",encoding="utf-8") as csv:
            read_file = csv.read()

    else:
        #print("{}".format(file))
        continue

    if regex1.findall(read_file) or regex2.findall(read_file) or regex3.findall(read_file):
        print ("YES, This file containts PHI")
        print(file)
    else:
        print("No, This file DOES NOT contain PHI")
        print(file)
© www.soinside.com 2019 - 2024. All rights reserved.