Created
July 17, 2017 09:52
-
-
Save c7h/cbba43f1c3d58250f9c9fc7bd39ebba7 to your computer and use it in GitHub Desktop.
major and annoying differences between module versions of Pythons JSON Library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Two versions of the same library behave completely different :-/ | |
the PI | |
``` | |
Python 3.4.2 (default, Oct 19 2014, 13:31:11) | |
[GCC 4.9.1] on linux | |
Type "help", "copyright", "credits" or "license" for more information. | |
>>> import json | |
>>> try: | |
... json.loads('notjson') | |
... except json.JSONDecodeError: | |
... print("didn't work") | |
... | |
Traceback (most recent call last): | |
File "<stdin>", line 2, in <module> | |
File "/usr/lib/python3.4/json/__init__.py", line 318, in loads | |
return _default_decoder.decode(s) | |
File "/usr/lib/python3.4/json/decoder.py", line 343, in decode | |
obj, end = self.raw_decode(s, idx=_w(s, 0).end()) | |
File "/usr/lib/python3.4/json/decoder.py", line 361, in raw_decode | |
raise ValueError(errmsg("Expecting value", s, err.value)) from None | |
ValueError: Expecting value: line 1 column 1 (char 0) | |
During handling of the above exception, another exception occurred: | |
Traceback (most recent call last): | |
File "<stdin>", line 3, in <module> | |
AttributeError: 'module' object has no attribute 'JSONDecodeError' | |
>>> | |
``` | |
the Mac: | |
``` | |
Python 3.5.0 (default, Sep 23 2015, 04:41:38) | |
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.72)] on darwin | |
Type "help", "copyright", "credits" or "license" for more information. | |
>>> import json | |
>>> try: | |
... json.loads("notjson") | |
... except json.JSONDecodeError: | |
... print("didn't work") | |
... | |
didn't work | |
>>> | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
JSONDecodeError
was introduced in 3.5https://docs.python.org/3/whatsnew/3.5.html#json
Use
ValueError
in older versions instead.