繁体   English   中英

无法使用Python解析目录中的多个文件

[英]Unable to parse multiple files in a directory Using Python

我的目录中有2个文件。 一个目录包含早晨ETL作业的开始和结束时间,另一个目录包含傍晚的相同数据。 我正在尝试编写一个Python程序来读取文件及其内容,并给出一个包含文件名,日期,开始时间和结束时间的excel输出。

我的代码如下:

path = r"path_name"
regex = '(.*?) - (.*?) - Starting entry (.*?)'
regex_1 = '(.*?) - (.*?) - Clear TMP table'
regex_2 = '(.*?) - (.*?) - Finished job'
for filename in glob.glob("*.log"):
    with open(filename, "r") as file:
        file_list = []
        table_list = []
        start_list = []
        end_list = []
        for line in file:
            line = line.replace('[','')
            line = line.replace(']','')
            line = line.replace('(','')
            line = line.replace(')','')
            for match in re.finditer(regex, line, re.S):
                match_text = match.group()
                print match_text
                searchfile = re.search(' - (.+?) - ', match_text)
                if searchfile:
                    filename = searchfile.group(1)
                    file_list.append(filename)
                    print(filename)
            for match in re.finditer(regex_1, line, re.S):
                match_text_1 = match.group()
                print match_text_1      
                searchtable = re.search(' - (.+?) - ', match_text_1)
                if searchtable:
                    tablename = searchtable.group(1)
                    table_list.append(tablename)
                    print(tablename)
                    starttime = match_text_1[0:19]
                    start_list.append(starttime)
                    print(starttime)
            for match in re.finditer(regex_2, line, re.S):
                match_text_2 = match.group()
                print match_text_2 
                endtime = match_text_2[0:19]
                end_list.append(endtime)
                print(endtime)

这里的问题是只读取和写入一个文件。 我不明白为什么会这样。 如果我要打印file_list的长度,它包含400行,但是理想情况下应该有800行,因为我正在解析2个文件。 有人可以帮我吗?

在循环外部初始化file_list ,然后使用append填充数据。

file_list = []
for filename in glob.glob('*.log'):
    if some_condition:
        file_list.append(filename)

在您的情况下,file_list在每次迭代中都会初始化,因此只有一半的数据存在。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM