Open SelwynChan opened 1 year ago
Sure, here's the modified function that prints the progress to the console:
import os
import csv
def map_logical_to_received_order_ids(directory):
mapping = {}
logical_order_ids_without_received = []
csv_files = [f for f in os.listdir(directory) if f.endswith(".csv")]
total_files = len(csv_files)
for idx, filename in enumerate(csv_files):
file_path = os.path.join(directory, filename)
with open(file_path, 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=chr(31))
headers = next(reader)
logical_order_idx = headers.index('logicalOrderID')
received_order_idx = headers.index('receivedOrderID')
for row in reader:
logical_order_id = row[logical_order_idx]
received_order_id = row[received_order_idx]
if received_order_id:
mapping[logical_order_id] = received_order_id
else:
logical_order_ids_without_received.append(logical_order_id)
progress = (idx + 1) / total_files * 100
print(f'Progress: {progress:.2f}%')
return mapping, logical_order_ids_without_received
# Usage example:
directory = "path/to/csv/files"
result, missing_received_order_ids = map_logical_to_received_order_ids(directory)
print("Mapping:", result)
print("Logical Order IDs without Received Order ID:", missing_received_order_ids)
In this version, the function calculates the progress percentage based on the number of processed files out of the total number of CSV files in the directory and prints it to the console. The tkinter progress bar has been removed.
To add a list of
logicalOrderID
s for which there was noreceivedOrderID
found, and use a tkinter progress bar, you can modify the function as follows:This modified function now initializes an empty list
logical_order_ids_without_received
to store thelogicalOrderID
s without correspondingreceivedOrderID
s. It also takes an additional argumentprogress_var
to update the progress bar.The function now returns a tuple containing the mapping and the list of
logicalOrderID
s without areceivedOrderID
.In the main part of the code, a tkinter progress bar is added. The
run_function_with_progress_bar
function is called when the user clicks the "Process CSV Files" button. This function sets up the progress bar and calls themap_logical_to_received_order_ids
function with the specified directory and progress bar variable.The progress bar is updated based on the number of processed files out of the total number of CSV files in the directory.