2

I wrote a simple html parsing class in python and it seems to work fine and then I try to use it with django and I get this error:

'ascii' codec can't decode byte 0xc2 in position 54465: ordinal not in range(128)

which is strange because I added this: # encoding: utf-8 to the top of my class. I don't really know much about encoding but can someone perhaps give me an idea of what's going here? Btw, I also insured that the source html was already in utf-8. Thanks!

3
  • # encoding: utf-8 won't do anything to how your class functions. It's just a comment... I think you may be confusing it with putting # -*- coding: utf-8 -*- at the top of the file, which simply tries to alert the parser (or text editor, etc) of the encoding of the source code. Commented Sep 22, 2011 at 20:20
  • well, I at least know for sure that it's a django issue and not a python issue. I try the class in python alone and it works fine. Then when I try to use it in django it returns an error that would indicate that it interprets everything in ascii. How can I make django think that everything is in utf-8? Update: nevermind I got it. See my answer below. Commented Sep 22, 2011 at 20:57
  • 1
    @voithos: # encoding: utf-8 and # -*- coding: utf-8 -*- have exactly the same effect. Python uses a regular expression to parse the encoding declaration; see docs.python.org/reference/… Commented Sep 22, 2011 at 21:22

2 Answers 2

2

Try putting that line at the top of your file. According to PEP 263, it has to be in the top two lines.

Sign up to request clarification or add additional context in comments.

2 Comments

You can also follow these instructions to make your encoding universal across all files: siafoo.net/snippet/32
Note that this will affect only string literals, not variables.
2

okay, I got it. All I needed to do was include # -*- coding: utf-8 -*- in the django view as well and that solved it!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.