Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用Python分析目录中的多个文件_Python_Python 3.x - Fatal编程技术网

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

无法使用Python分析目录中的多个文件,python,python-3.x,Python,Python 3.x,我在一个目录中有两个文件。一个目录包含早上ETL作业的开始和结束时间,另一个目录包含晚上的相同数据。 我试图编写一个Python程序来读取文件及其内容,并给出一个excel输出,其中包含文件名、日期、开始时间和结束时间 我的代码写在下面: path = r"path_name" regex = '(.*?) - (.*?) - Starting entry (.*?)' regex_1 = '(.*?) - (.*?) - Clear TMP table' regex_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)
这里的问题是只有一个文件被读取和写入。我不明白为什么会这样。如果我正在打印文件列表的长度,它包含400行,但理想情况下应该有800行,因为我正在解析2个文件。
有人能帮我吗?

在循环外部初始化
文件列表
,然后使用
追加
填充数据

i、 e


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

此代码不适用于缩进错误和不带括号的
print
。我怀疑这不是你的工作代码。请帮助他人解决这些问题。您现在可以尝试吗?这个编辑过的代码对我有用。
file_list = []
for filename in glob.glob('*.log'):
    if some_condition:
        file_list.append(filename)