为什么TQM进度条没有显示在我的csv阅读器循环中?

2024-04-25 08:53:47 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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中的行数),然后打印我的最后一行“完成分析文件”。已创建输出:',但输出csv为空。但是,如果我将total=len(list(read_obj))一起删除,脚本确实会运行,并且至少会显示发生的迭代次数和所用的时间,但不会显示进度条


Tags: csvthe进度条in脚本addobjread
1条回答
网友
1楼 · 发布于 2024-04-25 08:53:47
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)

相关问题 更多 >

    热门问题