繁体   English   中英

为什么我的 csv 阅读器循环上没有显示 tqdm 进度条?

[英]Why isn't the tqdm progress bar showing up on my csv reader loop?

我尝试了几种不同的方法,但到目前为止,我的代码中没有一种方法有效。 我最终破坏了我的脚本,所以它只显示一个空的进度并且脚本结束而不是实际执行它应该做的事情。 如何正确地将 tqdm 进度条添加到脚本的这一部分?

def add_column_in_csv(input_file, output_file, transform_row):
    # open input file and create output file
    with open(input_file, 'r') as read_obj, \
        open(output_file, 'wb') as write_obj:
        # create a csv.reader object from the input file object
        csv_reader = reader(read_obj)
        # create a csv.writer object from the output file object
        csv_writer = writer(write_obj)
        lines = len(list(read_obj))
        print lines
        # read each row of the input csv file as list
        for row in tqdm(csv_reader, total=len(list(read_obj))):
            # append the headers and values from checks in add_to_row
            transform_row(row, csv_reader.line_num)
            # add the updated row to the output file
            csv_writer.writerow(row)


# let them know it is doing something
print('Analyzing input file . . .')
# actually add the data to the new csv here
add_column_in_csv(input_file, output_file, add_to_row)
# it is done
print('Done analyzing file. Output created: ' + str(output_file))

这样做似乎不会在 for 循环中执行我的代码。 它在 0% 处显示一个没有进度的进度条,并显示 0/17(这是我的 csv 中的行数),然后打印我的最后一行“完成分析文件。 Output 创建:',但 output csv 为空白。 但是,如果我完全删除了 total=len(list(read_obj)) 脚本确实会运行,并且至少会显示发生的迭代次数和花费的时间,但没有进度条。

for row in tqdm(csv_reader, total=len(read_obj.read().split('\n'))):
    # append the headers and values from checks in add_to_row
            transform_row(row, csv_reader.line_num)
            # add the updated row to the output file
            csv_writer.writerow(row)

暂无
暂无

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

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