LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-29-2012, 03:15 AM   #1
monsteriname
Member
 
Registered: Sep 2009
Distribution: maggie 4 and 5
Posts: 35

Rep: Reputation: 0
python source code encoding


Reading the python primer at http://docs.python.org/2/tutorial/interpreter.html

I found that "It is possible to use encodings different than ASCII in Python source files."

My question;

Is there a group of python programmers that do normally change encoding?
If so, for what reason(s)?

I did google it, and found a lot of interesting info about encoding, but didn't see the more specific info I'm after.

Thanks,
 
Old 12-29-2012, 09:31 AM   #2
audriusk
Member
 
Registered: Mar 2011
Location: Klaipėda, Lithuania
Distribution: Slackware
Posts: 360

Rep: Reputation: 199Reputation: 199
This is done so often, that starting from Python 3 default encoding was changed to UTF-8. The reason is very simple: let's say you want to assign to a variable a Unicode string literal which contains some non-ASCII symbols in it. The example with Euro sign given in the docs section that you link to fits here perfectly:
Code:
foo = u'€'
If you want to write this line of code inside your .py file, you need to use an encoding that's different from ASCII and contains this symbol as well. But since Python interpreter treats string literals as ASCII text by default, you also need to inform it about the source file encoding by adding the following line at the beginning of your source file:
Code:
# -*- coding: utf-8 -*-
Otherwise you'll get an exception when trying to run your code:
Code:
  File "/tmp/foo.py", line 1
SyntaxError: Non-ASCII character '\xc5' in file /tmp/foo.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
 
1 members found this post helpful.
Old 12-29-2012, 10:24 PM   #3
monsteriname
Member
 
Registered: Sep 2009
Distribution: maggie 4 and 5
Posts: 35

Original Poster
Rep: Reputation: 0
Thank you audriusk for the great reply.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Could Python do sthg against Encoding Issues? Xeratul General 1 12-02-2012 08:18 AM
compile python source code with 64 bit linux partha82 Linux - Software 3 06-29-2010 06:00 PM
LXer: The Nature and Importance of Source Code and Learning Programming with Python LXer Syndicated Linux News 0 02-25-2010 02:50 PM
python problem - compiled from source - python -V still showing old version txm123 Linux - Newbie 1 02-15-2006 11:05 AM
LXer: Nokia's S60 Python Source Code Goes Open LXer Syndicated Linux News 0 02-04-2006 12:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration