import re # Function to transform each line def transform_line(line): # Use regex to extract parts of the line match = re.match(r'"([^"]+)","([^"]+)","\[(.*)\]"', line) if match: id_part = match.group(1) date_part = match.group(2) categories_part = match.group(3) # Remove extra quotes and split the categories categories = categories_part.replace('""', '"').split(',') # Swap categories order and join them with a semicolon transformed_categories = ','.join(categories[::-1]) # Return the transformed line return f'"{id_part}",{transformed_categories}' else: return None # Open input file and output file with open('export_hierarchy', 'r') as infile, open('export_hierarchy_transformed', 'w') as outfile: for line in infile: transformed_line = transform_line(line.strip()) if transformed_line: outfile.write(transformed_line + '\n')