Generation

generate functionSun, 18 Dec 2022

Replace re.split with regular split to get the same result (you can use the json library and the loads function) The code: f = open('purchase_log.txt', encoding='utf-8') import re purchases = {} for i, line in enumerate(f): line = re.split(r",|:", line.strip()) keys = line[1] values = line[3] purchases[keys] = values

def load_json(line): import json from json.decoder import JSONDecodeError try: line = json.loads(line) except JSONDecodeError: line = {} return line f = open('purchase_log.txt', encoding='utf-8') purchases = {} for i, line in enumerate(f): line = line.strip().split(',') keys = line[1] values = line[3] purchases[keys] = values

Questions about programming?Chat with your personal AI assistant