1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Rep Power

    Encoding errors in *comments*??

    I'm new to python. I wrote the following code, and it ran fine on one unix machine and on learnpython.org. Then I scp'd it to another machine, and I get encoding errors (below). I have searched and found many posts about encoding issues when using strings, and the use of the '# coding=utf-8" directive, however my code doesn't use any strings, it simply does a little math. Furthermore, the encoding errors seem to appear on *comment* lines. ??

    python Code:
    #! /usr/bin/python
    # This file is binsearch.py
    # in order to run it, you will need to do
    # "chmod u+x binsearch.py"
    # at the UNIX prompt to make it executable.
    # Then just run "./binsearch.py"
    # at the UNIX prompt
    def fofx(x):
       return x**3 - 4*x**2 + x + 2.0
    xlo = 0.0
    xhi = 10.0
    f = 999.999
    tol = 1.0e-12
    while (abs(f) > tol):
       xmid = (xlo+xhi)/2
       f = fofx(xmid)
       print xlo, xhi, xmid, f
       if (f > 0):
          xhi = xmid
          xlo = xmid

    The error message I get is...

    % ./binsearch.py
    File "./binsearch.py", line 3
    SyntaxError: Non-ASCII character '\xc2' in file ./binsearch.py on line 3, but no encoding declared; see (URL blocked: see forum rules) for details

    How can I change some python config file on this system to make these errors disappear? It's just a simple text file. I even copied & pasted via mouse-highlighting within a simple text editor in order to eliminate any potential "magic" invisible characters, but the error persists.

    If I add the '#coding=utf-8' line -- which I believe I "shouldn't" have to do, and this is why I'm posting this thread -- then I get a new error that seems to be due to the fact that I used spaces (3 spaces, exactly, for each indent) instead of tabs. I thought spaces OR tabs were allowed. Yay or nay?

    Thanks so much! I'm stumped...

    % python --version
    Python 2.7.3
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Rep Power
    The encoding error isn't anything to do with strings (that would be a UnicodeError at runtime, not a syntax error). It's a problem with the encoding of the source code itself -- the interpreter needs to be able to parse that, after all. By default, Python 2 expects ASCII, and you'll have to declare an encoding if that's not what you're using. (Python 3 uses UTF-8 as the source encoding by default.)

    That being said, I don't see any non-ASCII characters on line 3.

    As for tabs, yes, either tabs or spaces are accepted; you should only get an error if you're being inconsistent. Not really sure what's up there.
    Last edited by Nyktos; January 28th, 2013 at 06:51 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Rep Power
    I had that kind of trouble when switching between text editor (gedit and komodo for example). I think you should verify what is the default encoding in each of the text editors you used. Because even if you don't use any specific characters from ASCII or UT8, if the file is declared UT8 and is in fact ASCII, Python will bug.

    To avoid this, all my files start with :
    #! /usr/bin/env python
    # -*- coding: UTF-8 -*-

    Hope it helps.

IMN logo majestic logo threadwatch logo seochat tools logo