[PYTHON] Resolve UnicodeDecodeError in kivy JsonStore

Problem error

When dealing with json using storage module in Kivy, I ran into the following error.

   File "C:\programing\project\hoge\main.py", line 33, in _init_load
     self.store = JsonStore('test.json', )
   File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\storage\jsonstore.py", line 29, in __init__
     super(JsonStore, self).__init__(**kwargs)
   File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\storage\__init__.py", line 134, in __init__
     self.store_load()
   File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\storage\jsonstore.py", line 43, in store_load
     data = fd.read()
 UnicodeDecodeError: 'cp932' codec can't decode byte 0x82 in position 85: illegal multibyte sequence

It's a Unicode Decode Error seen from the face of the parent. Is it because it contains Japanese? This error can basically be resolved by adding encoding ='utf-8' to the argument of the open function. That's why the part that opens the json file in the storage module is changed quickly. Since there is a function to read on the 31st line, add a keyword argument to the open part there.

kivy/storage/jsonstore.py


def store_load(self):
        if not exists(self.filename):
            folder = abspath(dirname(self.filename))
            if not exists(folder):
                not_found = IOError(
                    "The folder '{}' doesn't exist!"
                    "".format(folder)
                )
                not_found.errno = errno.ENOENT
                raise not_found
            return
        with open(self.filename, encoding='utf-8') as fd:    #Here
            data = fd.read()
            if len(data) == 0:
                return
            self._data = loads(data)

result

It succeeded in reading normally. Japanese is confusing. .. ..

Recommended Posts

Resolve UnicodeDecodeError in kivy JsonStore
UnicodeDecodeError in pandas read_csv
I get a UnicodeDecodeError in mecab-python3
UnicodeDecodeError occurs in pip (Windows environment)