I have been trying to learn Python recently and following along with the book, Python for Data Analysis and using Python 2.7 with Canopy. In the book, they provided a link to some raw data which I saved and assigned to a path variable. After trying to convert the text file to a list of dictionaries using JSON:
records = [json.loads(line) for line in open(path)]
I received the following error:
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-17-b1e0b494454a> in <module>()
----> 1 records = [json.loads(line) for line in open(path)]
C:\Users\Marc\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.1.1975.win- x86_64\lib\json\__init__.pyc in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
336 parse_int is None and parse_float is None and
337 parse_constant is None and object_pairs_hook is None and not kw):
--> 338 return _default_decoder.decode(s)
339 if cls is None:
340 cls = JSONDecoder
C:\Users\Marc\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.1.1975.win- x86_64\lib\json\decoder.pyc in decode(self, s, _w)
363
364 """
--> 365 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
366 end = _w(s, end).end()
367 if end != len(s):
C:\Users\Marc\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.1.1975.win-x86_64\lib\json\decoder.pyc in raw_decode(self, s, idx)
379 """
380 try:
--> 381 obj, end = self.scan_once(s, idx)
382 except StopIteration:
383 raise ValueError("No JSON object could be decoded")
UnicodeDecodeError: 'utf8' codec can't decode byte 0x92 in position 6: invalid start byte
The weird thing is that this worked on a different computer, which I thought was using the same version of Python. Thanks in advance.
jsonmodule does decode, because the JSON standard specifies a (limited) number of encodings that are acceptable.open()call without explicitencodingparameter will indeed use the system locale to determine a default codec, but that doesn't apply here.