【问题标题】:Read all files in directory and output the files that contain certain regexes in them读取目录中的所有文件并输出其中包含某些正则表达式的文件
【发布时间】:2018-12-03 19:24:28
【问题描述】:

我正在尝试读取目录中的所有文件并输出包含正则表达式的文件以及每个文件中的正则表达式。

 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')

match_list=[]

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 = open(file,'rb')
            #print("{}".format(file))

    elif re.search(r".*(?=csv$)",file):
        #this is csv
        with open(file,"r+",encoding="utf-8") as csv:
            read_file = csv.read()
            #print("{}".format(file))
    elif re.search(r"/jupyter",file):
        print("wow")
    elif re.search(r"/scikit",file):
        print("wow")
    else:
        read_file = open(file, 'rb').read()
       #print("{}".format(file))
        continue
    if regex1.findall(read_file) or regex2.findall(read_file):
                print(read_file)

我设法编写了以下代码,但它给出了以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-39-f614d35e0441> in <module>()
     38        #print("{}".format(file))
     39         continue
---> 40     if regex1.findall(read_file) or regex2.findall(read_file):
     41                 print(read_file)

TypeError: expected string or bytes-like object

有什么方法可以让它在没有错误的情况下工作?

【问题讨论】:

  • the ones that contain the regexes 您的任期无效。这些文件实际上并不包含正则表达式。您正在使用正则表达式来匹配文件中的文本。
  • 你是什么意思?我正在尝试为两个正则表达式字符串过滤我的目录
  • 什么?你说你想在一个字符串中找到一个正则表达式?这是语义,正则表达式在 target 字符串上工作,而不是相反。
  • 我正在尝试匹配文件中的正则表达式模式

标签: python regex glob pypdf2 os.path


【解决方案1】:

用这个替换你读取的文件代码:

with open(File, mode='rb') as file:
    readFile = file.read()

【讨论】:

  • 我做了你的建议,但仍然得到以下错误:------------------------------- -------------------------------------------- TypeError Traceback(最近一次调用最后) in () 39 #print("{}".format(file)) 40 continue ---> 41 if regex1.findall(read_file) 或 regex2.findall( read_file): 42 email=regex1.findall(read_file) 43 print(email) TypeError: expected string or bytes-like object
  • 我希望您已将变量名从 read_file 更改为 readFile ?
  • 另外,仅出于测试目的,要求您注释所有代码并保留if 语句的1 块,看看..因为其余的都是一样的!!
  • 是的,我做到了。我已经做出了改变。仍然给出同样的错误。
  • pdf 循环中,您的 content 包含所有文本,您为什么还要打开 1 个文件...只需将该内容传递给您的正则表达式,它应该可以工作
【解决方案2】:

对于read(),只有open(filename) 可以工作。换成这个,你的问题就解决了。

read_file = open(file).read()

【讨论】:

    【解决方案3】:

    首先我向回答这个问题的其他人道歉,因为我会说一些关于 OP 以前的问题。

    关于OP,你不应该不假思索地复制代码。

    Content 是您已经阅读的页面。这意味着您的代码应该是read_file = content。以及为什么我写read_file = #,因为我认为你会添加额外的代码。但它不应该再次读取相同的文件。

    with open(file, 'rb') as pdfFileObj:
            pdfReader = PyPDF2.PdfFileReader(pdfFileObj) 
            pageObj = pdfReader.getPage(0)  
            content = pageObj.extractText()
            read_file = open(file,'rb') 
            #^---^---^ according to your former question, `read_file` should  be `content`
    

    而且还会出现其他问题。您应该在print("wow") 之后添加continue

    elif re.search(r"/jupyter",file):
        print("wow")
    elif re.search(r"/scikit",file):
        print("wow")
    

    否则您的代码将继续运行,然后发生错误。因为你什么都没读。

    if regex1.findall(read_file) or regex2.findall(read_file):
        print(read_file)
    

    【讨论】:

    • 你是对的..这就是我告诉 OP 的话,如果你在下面阅读我的答案!
    • 你可以聊天吗?你的答案没有意义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多