0

i have a problem. I use collectstatic for production. But i have this problem with a css file. UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe1 in position 240647: invalid continuation byte

But, i don't know why.

This is the traceback:

Post-processed 'tiempo_turco/stylesheets/foundation.css' as 'tiempo_turco/stylesheets/foundation.6f8a1d5c4dbc.css'
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/tulipan/Proyectos/IspanyolHaber/lib/python3.4/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/home/tulipan/Proyectos/IspanyolHaber/lib/python3.4/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/tulipan/Proyectos/IspanyolHaber/lib/python3.4/site-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/tulipan/Proyectos/IspanyolHaber/lib/python3.4/site-packages/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "/home/tulipan/Proyectos/IspanyolHaber/lib/python3.4/site-packages/django/core/management/base.py", line 415, in handle
return self.handle_noargs(**options)
File "/home/tulipan/Proyectos/IspanyolHaber/lib/python3.4/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 173, in handle_noargs
collected = self.collect()
File "/home/tulipan/Proyectos/IspanyolHaber/lib/python3.4/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 119, in collect
for original_path, processed_path, processed in processor:
File "/home/tulipan/Proyectos/IspanyolHaber/lib/python3.4/site-packages/django/contrib/staticfiles/storage.py", line 251, in post_process
content = original_file.read().decode(settings.FILE_CHARSET)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe1 in position 240647: invalid continuation byte

Thank you very much for your help!

UPDATE:

I try fix it deleting this css file, but, i deleted and i have the same error, i don't understand why!

SalahAdDin
  • 2,023
  • 25
  • 51
  • Are you sure that the CSS file is encoded as UTF8? It appears not to be. – mhawke Jul 16 '14 at 00:24
  • The utf8 codec is seeing a continuation character (0xe1) in the CSS file which it expects is part of a UTF8 byte sequence. Confirm that the CSS file is the problem by replacing it with a simpler file containing ASCII only (a subset of UTF8). Look in the file around byte 240647. – mhawke Jul 16 '14 at 01:04
  • Type the following into your python interpreter (you might need the full path to the css file): `open('tiempo_turco/stylesheets/foundation.css').read()[240645:240650]` - what is displayed? – mhawke Jul 16 '14 at 03:21
  • Yes... you need to use Python - it's not a bash script. Anyway look at my answer which guesses at the CSS file's encoding and converts it to UTF8. – mhawke Jul 16 '14 at 03:51
  • >>> open("foundation.css").read()[240645:240650] '' >>> open('foundation.css').read()[240645:240650] '' >>> – SalahAdDin Jul 16 '14 at 03:58
  • I try this now: >>> open('statics/static/tiempo_turco/stylesheets/foundation.css') – SalahAdDin Jul 16 '14 at 04:05
  • I think you are beyond help. Please follow the code and/or bash commands provided. – mhawke Jul 16 '14 at 04:07
  • Forget it, the problem was a file with an á character. – SalahAdDin Mar 29 '16 at 01:56

3 Answers3

1

Probably your CSS file is not really encoded as UTF8, most likely it is ISO-8859-1 in which the byte 0xE1 equates to á (LATIN SMALL LETTER A WITH ACUTE). You can check the file type with the file command, and then convert it to UTF8 using iconv:

$ cp tiempo_turco/stylesheets/foundation.css /tmp
$ file /tmp/foundation.css
/tmp/foundation.css: ISO-8859 text
$ iconv -f ISO-8859-1 -t UTF8  /tmp/foundation.css >/tmp/foundation_utf8.css
$ file /tmp/foundation_utf8.css
/tmp/foundation_utf8.css: UTF-8 Unicode text

Not sure about how to update the file in your installation - you said that you tried removing it without any change, so maybe you need to restart your server?

If you don't have iconv you can convert it to UTF8 in Python:

$ python
>>> css = open('/tmp/foundation.css').read().decode('iso-8859-1')
>>> open('/tmp/foundation_utf8.css', 'w').write(css.encode('utf8'))
mhawke
  • 84,695
  • 9
  • 117
  • 138
  • If it really is ASCII then you should have no UTF8 decoding issues. Can you make your CSS file available somewhere? e.g. pastebin.com – mhawke Jul 16 '14 at 04:40
  • My brother, i find finally the error, are in other file, app.css, i found it using the code here: [link]http://stackoverflow.com/questions/11915432/django-execute-command-collectstatic-raise-unicodedecodeerror The first answer. – SalahAdDin Jul 16 '14 at 07:44
  • The code i put before this: `# ..to apply each replacement pattern to the content if name in adjustable_paths:` In the line 249 in the file. And print me what is file error, and voila, i used your commands: `>>> open('statics/static/tiempo_turco/stylesheets/app.css').read()[240645:240650] ' P\xe1gi' >>> open('statics/static/tiempo_turco/stylesheets/app.css').read()[240605:240650] ';\n}\n\n/* Estilos de Slider de Noticias en P\xe1gi'` Yes, i search in this file and changed bad character and works!!! Thank you very much for your help! – SalahAdDin Jul 16 '14 at 07:44
  • Yes, the problem is it. – SalahAdDin Mar 29 '16 at 01:56
1

I had the same error when I used django-pipeline inside docker container. It turned out that for some reason the system used POSIX locale. I used the solution proposed here and exported locale setting in system shell:

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

It worked well. Also, notice I did that both in docker and outside machine.

Dmytriy Voloshyn
  • 1,032
  • 12
  • 27
0

Django has a good function for converting strings to unicode. Try out this function

from from django.utils.encoding import smart_unicode

smart_unicode(value)
ZJS
  • 3,991
  • 2
  • 15
  • 22
  • when you save the css file use this when your are writing a string to the file... write(smart_unicode(s)) – ZJS Jul 16 '14 at 01:36