[Django]-UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0

9👍

I had the same problem recently… here’s my solution:

Confirm the encoding format of ‘file_path’:

  1. download and open the file with Notepad++
  2. check the lower right corner; there you can see whether your file was encoded in the compatible format, or if it has the Byte Order Marker or BOM sign,
  3. if either of these is true, simply ‘save as’ the correct/required format.

You should be fine with the above.

1👍

Just go on the location of file from Where the error is coming.

In my case the error was coming from json.py file in serializers, because my error is like:

File "C:\Users\User\AppData\Local\Programs\Python\Python38\Lib\site-packages\django\core\serializers\json.py",line 66 in Deserializer stream_or_string = stream_or_string.decode() UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

After going to location , just open json.py file with your idle or notepad, so that you can edit that file. Just go in Deserializer function on the line:

stream_or_string = stream_or_string.decode()

and change this line to

stream_or_string = stream_or_string.decode('UTF-16')

and save it,Now your error will be solved.

1👍

Partly with Abrahams solution above..

If your using VS Code, look in the lower right hand corner and it might say something like UTF-<xx>. Mine is in UTF-16. Simply click on it, then click ‘save with encoding’, and select ‘utf-8’. I keep forgetting that certain commands from the CLI, when creating files, will create them as UTF-8 (certain commands when running in powershell) $PSDefaultParametervalues['Out-File:Encoding'] = 'utf8'

Leave a comment