0
import ast

dict_from_file=[]
with open('4.txt', 'r') as inf:
    dict_from_file = ast.literal_eval(inf.read())

File "<unknown>", line 1
["hello":"work", "please":"work"]
  ^
SyntaxError: invalid character in identifier

Hi Everyone! The above is my code and my error. I have a really complicated 40MB data file in the form of a dictionary to work on, but couldn't get that import to work so tried a simple one.

I'm using the latest Jupyter notebook from the latest version of Anaconda, on Windows 10. My dictionary is a txt file created using windows notepad. The complicated dictionary was originally a JSON file that I changed into a txt file thinking it would be easier but I may be wrong.

I think the above error is an encoding issue? But not sure how to fix it.

Thanks!

  • 3
    Try this: `import json; dict_from_file = json.load(open('4.txt', 'r', encoding='utf-8'))` – cs95 Jul 26 '17 at 11:18
  • @cᴏʟᴅsᴘᴇᴇᴅ You mean because that results in a better error message? – Stefan Pochmann Jul 26 '17 at 11:22
  • @StefanPochmann yes. :p – cs95 Jul 26 '17 at 11:23
  • 1
    "The complicated dictionary was originally a JSON file that I changed into a txt file thinking it would be easier but I may be wrong."-- yes, you're likely wrong, just keep the original file – Chris_Rands Jul 26 '17 at 11:24
  • That `""` is actually the notorious [UTF-8 Byte Order Mark](https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8), `b'\xef\xbb\xbf'`. As Wikipedia says: "The Unicode Standard permits the BOM in UTF-8, but does not require or recommend its use"; the Windows notepad utility insists on using it. So you need to skip over those 3 bytes. You can do that by doing `inf.read(3)` before the `dict_from_file = ast.literal_eval(inf.read())` line. – PM 2Ring Jul 26 '17 at 11:38
  • However, if the file was originally proper JSON, then you should use the `json` module functions to read & write it, rather than messing around with manual conversion in Notepad and evaluating the resulting text file with `ast.literal_eval`. – PM 2Ring Jul 26 '17 at 11:49

2 Answers2

0

If you are the owner/write of the file (dict formated), save as json

import json

#To write
your_dict = {.....}
with open("file_name.txt", "w+") as f:
    f.write(json.dumps(your_dict)

#To read
with open("file_name.txt") as f:
    read_dict = json.load(f)
Wonka
  • 1,548
  • 1
  • 13
  • 20
  • If this is the case you can also use `Pickle`. To determine which is better for your case read this accepted anwser https://stackoverflow.com/questions/2259270/pickle-or-json – user3053452 Jul 26 '17 at 11:25
  • @Stefan Pochmann well pickle is better if you have dict with floats and you're going to use said dict only in Python. In accepted anwser it is stated that JSON is better suited for interoperability. – user3053452 Jul 26 '17 at 11:32
  • I went back to the original json and got that to work with this code: import json read_dict=[] with open("3.json") as f: read_dict = json.load(f) Thanks! – Harshil Sumaria Jul 26 '17 at 15:23
  • You are welcome @Harshil Sumaria, vote and accept answer to close you ask – Wonka Jul 26 '17 at 20:01
0

This is possibly a python 3 "feature". This code removes the unwanted characters at the start of the input file and returns the input data as type string.

with open('4.txt', 'r',,encoding="utf-8-sig") as inf:
    dict_from_file = ast.literal_eval(inf.read())

This removes the strange characters put at the beginning of read data.

Keir
  • 557
  • 1
  • 6
  • 17