utf-8 codec can t decode byte 0xa9 in position 12527 invalid start byte

+1 vote

while deploying my django localhost code to digital ocean, i am getting this error.

'utf-8' codec can't decode byte 0xa9 in position 12527: invalid start byte
showing below error
home/mesite/djangoprojectdir/homeapp/views.py in index
return render(request, "index.html", {'slide': slide})


/usr/lib/python3.6/codecs.py in decode
  1.         (result, consumed) = self._buffer_decode(data, self.errors, final)

Jun 26, 2020 in Python by Arun
• 130 points
6,582 views

Hi, @There,

It will be helpful if you could post your code here for better understanding.

http://139.59.7.210:8080/

Hi @Gitika, thanks for your reply, please check above link for more details.

Environment:

Request Method: GET
Request URL: http://139.59.7.210:8080/

Django Version: 2.2.2
Python Version: 3.6.9
Installed Applications:
['homeapp.apps.HomeappConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'news',
 'yogablog',
 'ckeditor',
 'social_django',
 'spiritual',
 'babynames',
 'generalnews',
 'django_inlinecss',
 'timezone_field']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Traceback:

File "/home/mesite/djangoprojectdir/djangoprojectenv/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/home/mesite/djangoprojectdir/djangoprojectenv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "/home/mesite/djangoprojectdir/djangoprojectenv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/mesite/djangoprojectdir/homeapp/views.py" in index
  11.     return render(request, "index.html", {'slide': slide})

File "/home/mesite/djangoprojectdir/djangoprojectenv/lib/python3.6/site-packages/django/shortcuts.py" in render
  36.     content = loader.render_to_string(template_name, context, request, using=using)

File "/home/mesite/djangoprojectdir/djangoprojectenv/lib/python3.6/site-packages/django/template/loader.py" in render_to_string
  61.         template = get_template(template_name, using=using)

File "/home/mesite/djangoprojectdir/djangoprojectenv/lib/python3.6/site-packages/django/template/loader.py" in get_template
  15.             return engine.get_template(template_name)

File "/home/mesite/djangoprojectdir/djangoprojectenv/lib/python3.6/site-packages/django/template/backends/django.py" in get_template
  34.             return Template(self.engine.get_template(template_name), self)

File "/home/mesite/djangoprojectdir/djangoprojectenv/lib/python3.6/site-packages/django/template/engine.py" in get_template
  143.         template, origin = self.find_template(template_name)

File "/home/mesite/djangoprojectdir/djangoprojectenv/lib/python3.6/site-packages/django/template/engine.py" in find_template
  125.                 template = loader.get_template(name, skip=skip)

File "/home/mesite/djangoprojectdir/djangoprojectenv/lib/python3.6/site-packages/django/template/loaders/base.py" in get_template
  24.                 contents = self.get_contents(origin)

File "/home/mesite/djangoprojectdir/djangoprojectenv/lib/python3.6/site-packages/django/template/loaders/filesystem.py" in get_contents
  24.                 return fp.read()

File "/usr/lib/python3.6/codecs.py" in decode
  321.         (result, consumed) = self._buffer_decode(data, self.errors, final)

Exception Type: UnicodeDecodeError at /
Exception Value: 'utf-8' codec can't decode byte 0xa9 in position 12527: invalid start byte

1 answer to this question.

+1 vote

Hello,

You can always safely read in binary mode and decode it in utf8 with ignore mode. 

In Python something like this:

with open(filename, 'rb') as f:
     lines = [l.decode('utf8', 'ignore') for l in f.readlines()]

OR try this:

data = pd.read_csv("your.csv", encoding='cp1252')

Hope this is helpful!

Thank You!

answered Jun 30, 2020 by Niroj
• 82,820 points

Related Questions In Python

0 votes
0 answers

utf-8' codec can't decode byte 0xa0 in position 10: invalid start byte

my code import wordcloud import numpy as np from matplotlib ...READ MORE

Mar 29, 2020 in Python by anonymous
• 120 points
2,873 views
0 votes
2 answers
0 votes
1 answer

'utf-8' codec can't decode byte 0x82 in position 16: invalid start byte

Hi@zena, The error is because there is some non-ASCII ...READ MORE

answered Jun 29, 2020 in Python by MD
• 95,300 points
3,803 views
0 votes
2 answers

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte

Hey,  @Himanshu. It's still most likely gzipped data. ...READ MORE

answered Jul 27, 2020 in Python by Gitika
• 65,950 points
6,499 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,421 views
0 votes
0 answers
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,810 points
1,820 views
+2 votes
2 answers

UnicodeDecodeError: "utf-8" codec can't decode byte in position : invalid start byte

You have to use the encoding as latin1 ...READ MORE

answered Jul 23, 2019 in Python by Kunal
134,757 views
0 votes
1 answer

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte

Hi, @hala, Regarding your query, you can go ...READ MORE

answered Jun 29, 2020 in Python by Niroj
• 82,820 points
7,556 views