import csv def update_user_profile(user_data, profile_wifi_ssid): """ Updates user profile information in the Users.csv file. Args: user_data (dict): A dictionary containing user profile data (e.g., 'User', 'User_pass', etc.). profile_wifi_ssid (str): The Wifi_SSID of the user to update. """ with open('data/Users.csv', "r", encoding="utf-8") as file: csv_reader = csv.DictReader(file, delimiter=",") rows = [row for row in csv_reader] for row in rows: if row['Wifi_SSID'] == profile_wifi_ssid: row['User'] = user_data['User'] row['User_pass'] = user_data['User_pass'] row['Wifi_SSID'] = user_data['Wifi_SSID'] row['Modbus_IP'] = user_data['Modbus_IP'] row['Modbus_Port'] = user_data['Modbus_Port'] # The original code had 'Reserve' overwritten twice. Assuming it's a single 'Reserve' column. # If there are two distinct 'Reserve' fields (e.g., branch and semester), the CSV header and logic need clarification. # For now, I'll use the last assigned value for 'Reserve' as per original logic. row['Reserve'] = user_data['Reserve_branch'] # Assuming this is for branch row['Reserve'] = user_data['Reserve_semester'] # This will overwrite the previous 'Reserve' value break with open('data/Users.csv', newline="", mode="w", encoding="utf-8") as file: # The original header had two 'Reserve' columns. This is unusual for CSV and might be a typo. # I will use a single 'Reserve' column as it's more common, or clarify if two are truly needed. # Based on the original code, it seems like the second 'Reserve' assignment overwrites the first. header = ["User", "User_pass", "Wifi_SSID", "Modbus_IP", "Modbus_Port", "Reserve"] csv_writer = csv.DictWriter(file, fieldnames=header) csv_writer.writeheader() csv_writer.writerows(rows)