Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A source file with unicode characters is making Django throw up a SyntaxError exception

A file in UTF-8 encoding has an è character (e with accent grave) embedded in comment delimiters for Python. Django complains about this character and will not render the page. How can I resolve this?

like image 775
canadadry Avatar asked Jul 25 '11 06:07

canadadry


People also ask

How do you handle Unicode errors?

The Python "SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position" occurs when we have an unescaped backslash character in a path. To solve the error, prefix the path with r to mark it as a raw string, e.g. r'C:\Users\Bob\Desktop\example. txt' .

Does Python accept Unicode?

Python's string type uses the Unicode Standard for representing characters, which lets Python programs work with all these different possible characters.

What is Unicode escape?

A unicode escape sequence is a backslash followed by the letter 'u' followed by four hexadecimal digits (0-9a-fA-F). It matches a character in the target sequence with the value specified by the four digits. For example, ”\u0041“ matches the target sequence ”A“ when the ASCII character encoding is used.

What character is x80?

x80 - xFF are non-ASCII character ranges. They're still printable, both in Latin-1, or encode higher code points for UTF-8. Using \\x80 over \x80 is slightly more correct. The backslash escapes itself in strings.


2 Answers

The SyntaxError Django is raising already points you in the right direction.

It is always a good thing to actually read exceptions. In your case, it will have said something along the lines of

Non-ASCII character '\xc3' in file /home/zakx/../views.py on line 84, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details (views.py, line 84)

If you'd then read PEP-0263, you could learn that there are some ways to tell Python (and your editor!) which encoding your files are in. Generally, you will want to use UTF-8 encoding whenever possible. Therefore, writing one of the following lines into the first line (or second, if you use a shebang) will tell Python to use UTF-8 for that file.

# coding=utf8
# -*- coding: utf8 -*-
# vim: set fileencoding=utf8 :
like image 180
zakx Avatar answered Sep 19 '22 08:09

zakx


Did you try adding the coding header to the file? On the first line, possibly after the shebang line, add

# -*- coding: utf-8 -*-
like image 45
agf Avatar answered Sep 19 '22 08:09

agf