I have a .txt file (type string) {'A': 0, 'B': 2, 'C':0, ...} which I would like to convert to a dictionary (to be able to save all elements containing 0s in another file).
I have found many codes, but in them key to be and value to be are separated with a comma, not : ( I always receive error message invalid syntax on :).
I have tried to convert the string into a list, and convert this list into a dictionary, but did not manage...
with open("ab.txt", 'r') as f:
filecontents = f.read().replace(",", "\n")
filecontents.replace(":", ",") # this does not work
res = filecontents.strip('{}').split('\n')
Do you have any suggestions how to do the conversion? (or just save the elements containing 0 differently)?