【发布时间】:2020-05-14 14:39:42
【问题描述】:
我尝试了几种不同的方法,但到目前为止,我的代码都没有。我最终破坏了我的脚本,所以它只显示一个空的进度并且脚本结束而不是实际做它应该做的事情。如何在脚本的这一部分正确添加 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 中的行数),然后打印我的最后一行“完成分析文件。输出已创建:',但输出 csv 为空白。但是,如果我完全删除 total=len(list(read_obj)) 脚本确实会运行,并且至少会显示发生的迭代次数和花费的时间,但没有进度条。
【问题讨论】: