嵌套循环中的Python进度栏

时间:2018-12-01 19:53:36

标签: python python-3.x tqdm

我正在将一些Linux日志数据转换为CSV以便进行数据分析。有些说明需要一些时间,因此,我想我将为每个正在翻译的文件放在一个进度栏中。但是,当在带有progresspar2或tqdm的进度条中放置时,我的熊猫数据框为null。完全没有数据。当我删除进度条时,一切都会正常进行。

这是我的CSV翻译功能:

import pandas as pd
from dateutil import parser
from tqdm import trange
import os
import glob
import csv
import socket


def logsToCSV():
print("[+] Translating log to CSV")
log_file = open(CSV_FILE_PATH, "w", newline='')
csv_w = csv.writer(log_file)

for filename in glob.glob(os.path.join(LOGS_FILE_PATH, '*.txt')):       # Find all files in path with .txt
    data_file = open(filename, "r")
    file_length = len(data_file.readlines())

    for i in trange(file_length, desc='loop', leave=False):             # Progress Bar Via TQDM
        for new_line in data_file:

            new_line = line.strip().split(" ")
            date = str("%s %s %s" % (new_line[0], new_line[1], new_line[2])).strip()
            date = parser.parse(date)
            ip =str(new_line[5]).partition("/")
            ip = str(ip[0]).strip()
            try:
                url = str(new_line[7]).strip()
            except:
                url = None
            csv_w.writerow([date,ip,url])

enter image description here

TQDM正在破坏某些东西,或者我没有正确实现它。

编辑1:

我知道了。我用尽了在readlines()中读取的文件以获取长度。这有效:

def logsToCSV():
print("[+] Translating log to CSV")
log_file = open(CSV_FILE_PATH, "w", newline='')
csv_w = csv.writer(log_file)
path, dirs, files = next(os.walk(LOGS_FILE_PATH))
log_num = len(files)
print(log_num)

for filename in glob.glob(os.path.join(LOGS_FILE_PATH, '*.txt')):       # Find all files in path with .txt
    data_file = open(filename, "r")
    with open(filename, "r") as f:
        file_length = len(f.readlines())
    f.close()
    pbar = tqdm(total=file_length)
    for line in data_file:         
        new_line = line.strip().split(" ")
        date = str("%s %s %s" % (new_line[0], new_line[1], new_line[2])).strip()
        date = parser.parse(date)
        ip =str(new_line[5]).partition("/")
        ip = str(ip[0]).strip()
        try:
            url = str(new_line[7]).strip()
        except:
            url = None
        csv_w.writerow([date,ip,url])
        pbar.update(1)
    pbar.close()

1 个答案:

答案 0 :(得分:-1)

您可以将tqdm应用于主循环:

从tqdm导入tqdm 对于我在tqdam中(条件):