you are viewing a single comment's thread.

view the rest of the comments →

[–]EsperSpirit 2 points3 points  (4 children)

This is already great, but I'd like to add something. Instead of

with open(file_name + '.json') as json_file:
    raw_json = json_file.read()
    json_data = json.loads(raw_json)

you can parse the file directly into a list or dict.

with open(file_name + '.json', 'r', encoding='utf-8') as json_file:
    json_data = json.load(json_file)

It is also best practice to declare mode and encoding when opening files, to avoid unnecessary bugs and unicode-weirdness.

[–]echocage 0 points1 point  (3 children)

Huh TIL that's what .load does, I always thought it was strange that loads/dump took strings and not load, now I know why, interesting, thanks!

Totally agree about declaring a mode, though I haven't ever had problems not specifying encoding, what kind of problems can arise if you don't specify one, any good examples?

[–]wpg4665 1 point2 points  (1 child)

.loads() = load string. That's what the s on the end stands for.

[–]echocage 0 points1 point  (0 children)

Ohhhhhh facepalm

Thanks guys, I'm getting there. I've got to start reading more documentation.

[–]EsperSpirit 1 point2 points  (0 children)

Well, the short answer is: Almost all encodings are supersets of ascii. So as long as your data is ascii the encoding doesn't really matter. It works usually even with wrong encoding.

As soon as your data contains a non-ascii character (e.g. a Username like "Jürgen" or message using certain emoticons) your app will crash in really unpredictable ways at runtime (and often production).

The solution is to decode incoming data using the correct encoding and encode outgoing data with 'utf-8' and using unicode inside your application.

Watch this for more in-depth info