0

How to parse a text file using python dictionary

Key=value|key=value| Pls note that line end with a '|'

10th Nov 2016, 12:30 PM
Dinesh Das
Dinesh Das - avatar
1 Odpowiedź
+ 2
this is in python 3 this expects file to be in key=value| format eg: key1="value1"|key2=500|hello=("how","are","you")| if you need data to be only strings like in key1=value1|key2=hello| delete import ast and replace dict_var[key]=ast.literal_eval(value) with dict_var[key]=value import ast dict_var={} try: f = open("text/file/path") content=f.read().split("|") f.close() content=content[:-1] for item in content: key,value=item.split("=") dict_var[key]=ast.literal_eval(value) # if you want print(dict_var) except Exception as e: print(e)
10th Nov 2016, 1:33 PM
Sunera Avinash
Sunera Avinash - avatar