Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 28th, 2013, 04:41 PM
doctorh doctorh is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 1 doctorh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 38 m 14 sec
Reputation Power: 0
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:
Original - python Code
  1. #! /usr/bin/python
  2. # This file is binsearch.py
  3. # in order to run it, you will need to do
  4. # "chmod u+x binsearch.py"
  5. # at the UNIX prompt to make it executable.
  6. # Then just run "./binsearch.py"
  7. # at the UNIX prompt
  8.  
  9. def fofx(x):
  10.    return x**3 - 4*x**2 + x + 2.0
  11.  
  12. xlo = 0.0
  13. xhi = 10.0
  14. f = 999.999
  15. tol = 1.0e-12
  16. while (abs(f) > tol):
  17.    xmid = (xlo+xhi)/2
  18.    f = fofx(xmid)
  19.    print xlo, xhi, xmid, f
  20.    if (f > 0):
  21.       xhi = xmid
  22.    else:
  23.       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

Reply With Quote
  #2  
Old January 28th, 2013, 06:49 PM
Nyktos Nyktos is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 88 Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 5 h 29 m 46 sec
Reputation Power: 2
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.

Reply With Quote
  #3  
Old January 29th, 2013, 12:41 AM
chaying chaying is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2010
Posts: 14 chaying User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 44 m 9 sec
Reputation Power: 0
Hi
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Encoding errors in *comments*??

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap