python - Syntax Error when importing a text file containing a dictionary -
import ast dict_from_file=[] open('4.txt', 'r') inf: dict_from_file = ast.literal_eval(inf.read()) file "<unknown>", line 1 ["hello":"work", "please":"work"] ^ syntaxerror: invalid character in identifier hi everyone! above code , error. have complicated 40mb data file in form of dictionary work on, couldn't import work tried simple one.
i'm using latest jupyter notebook latest version of anaconda, on windows 10. dictionary txt file created using windows notepad. complicated dictionary json file changed txt file thinking easier may wrong.
i think above error encoding issue? not sure how fix it.
thanks!
if owner/write of file (dict formated), save json
import json #to write your_dict = {.....} open("file_name.txt", "w+") f: f.write(json.dumps(your_dict) #to read open("file_name.txt") f: read_dict = json.load(f)
Comments
Post a Comment