I have tried doing it a few different ways, but none have worked so far within my code. I end up breaking my script so it just shows an empty progress and the script ends instead of actually doing what it is supposed to. How do I properly add a tqdm progress bar to this part of my script?
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))
Doing it like this it seemingly doesn't execute my code within the for loop. It shows a progress bar at 0% that does not progress and says 0/17 (which is the number of rows in my csv) and then prints my last line 'Done analyzing file. Output created:', but the output csv is blank. However, if I remove the total=len(list(read_obj)) altogether the script does run and at least shows the number of iterations happening and how long it took, but no progress bar.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.