473,387 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

UTF-8

First off: i thoroughly enjoy python. I use it for scientific
computing with scipy, numpy and matplotlib and it's an amazingly
efficient and elegant language.

About this mailing list: it is very hard to search. I can't find any
search field on the page: http://mail.python.org/mailman/listinfo/
python-list. I would greatly appreciate if you moved that list over
to google, for instance, so that it be both searchable and rss-feedable.

My question is the following: how to set a default encoding in
python? I read an old thread about that and it didn't seem possible
by then.

The default encoding i wish to set is UTF-8 since it encodes unicode
and is nowadays the standard encoding. Having ascii as a default
encoding is a pretty strange choice since ascii encodes only 127
characters (suitable only for English, without fancy symbols like €)
whereas utf-8 encodes virtually infinitely many characters (suitable
for all languages). Besides, utf-8 is a superset of ascii so having
it by default won't harm anything.

At the very least there should be a configuration file that allows to
set a default encoding (although setting anything else than utf-8
should be advised against). Such a feature would be greatly
appreciated by all of us non native English speakers around the world.

Best regards,

== Olivier
Mar 10 '07 #1
4 1794
Olivier Verdier wrote:
<zip>
My question is the following: how to set a default encoding in
python? I read an old thread about that and it didn't seem possible
by then.
You *can* put a sys.setdefaultencoding("utf-8") in your sitecustomize.py
(see Python libs/site-packages/). Note that this function is no longer
available after Python startup.

But, IMHO, this is a *bad idea*, it may break modules which consider default
encoding to be ascii.
You should prefer to put
# -*- coding: utf-8 -*-
at the begining of your sources files. With that you are ok with all Python
installations, whatever be the defautl encoding.
Hope this will become mandatory in a future Python version.

A+

Laurent.

Mar 10 '07 #2
Laurent Pointal wrote:
You should prefer to put
# -*- coding: utf-8 -*-
at the begining of your sources files. With that you are ok with all Python
installations, whatever be the defautl encoding.
Hope this will become mandatory in a future Python version.
The default encoding specifies how Unicode strings are implicitly
converted into byte strings. The code you gave specifies how Unicode
string literals in a file are encoded, which is something completely
different.
Mar 10 '07 #3
On Mar 11, 1:00 am, Olivier Verdier <cht...@gmail.comwrote:
First off: i thoroughly enjoy python. I use it for scientific
computing with scipy, numpy and matplotlib and it's an amazingly
efficient and elegant language.

About this mailing list: it is very hard to search. I can't find any
search field on the page:http://mail.python.org/mailman/listinfo/
python-list. I would greatly appreciate if you moved that list over
to google, for instance, so that it be both searchable and rss-feedable.
The mailing list and the Usenet newsgroup comp.lang.python mirror each
other. Google Groups provides access to Usenet newsgroups; try
http://groups.google.com/group/comp.lang.python

Other possibilities are gmane.org and Yahoo Groups; see
http://www.python.org/community/lists/

HTH,
John


Mar 10 '07 #4
On Sat, 10 Mar 2007 15:00:04 +0100, Olivier Verdier <ch****@gmail.com>
wrote:
[snip]
The default encoding i wish to set is UTF-8 since it encodes unicode and
is nowadays the standard encoding.
I can't agree with that: there are still many tools completely ignoring
the encoding problem, and just saving sequences of bytes for any text.
This results in letting the current OS deciding what encoding is used, and
this is still not always UTF-8. Just consider how many web pages are still
encoded in a local encoding without even any specification of what it may
be in the HTML header, or how many times you see a [?] or a black square
replacing a character in newsgroups or mailing lists. UTF-8 *should* be
the standard encoding, but certainly isn't...
Having ascii as a default encoding is a pretty strange choice since
ascii encodes only 127 characters (suitable only for English, without
fancy symbols like €) whereas utf-8 encodes virtually infinitely many
characters (suitable for all languages). Besides, utf-8 is a superset of
ascii so having it by default won't harm anything.
On the contrary, it's IMHO a very sensible thing to do: any character with
a code 127 could be in any encoding. So supposing an encoding would be
guessing, and part of the Python Zen is: "in the face of ambiguity, refuse
the temptation to guess". The problem is here that if the encoding is not
UTF-8, things can seem to work, but give completely wrong results. So the
decision was apparently to use the minimal common set for everything,
which is ASCII; this seems quite logical to me.

(BTW, one could even argue that the default encoding should be
non-existent, as I think there are some non-latin encodings that do not
even include ASCII [I think I saw a Chinese encoding like that one
day...]. So even supposing that ASCII will work is sometimes wrong...)
At the very least there should be a configuration file that allows to
set a default encoding (although setting anything else than utf-8 should
be advised against). Such a feature would be greatly appreciated by all
of us non native English speakers around the world.
Well, I *am* a non-native English speaker, and after the first "why should
I bother about all this encoding stuff?" period, which seems to happen to
everybody, I finally understood the logic behind Python choices, and why I
*should* bother about encodings. Whatever the default encoding is, and
whatever you do, encodings will still be a problem for some time. The
transition phase from local encodings to universal ones is likely to last
a few years more, if not a few decades...

HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
Mar 12 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: lawrence | last post by:
Someone on www.php.net suggested using a seems_utf8() method to test text for UTF-8 character encoding but didn't specify how to write such a method. Can anyone suggest a test that might work?...
4
by: Alban Hertroys | last post by:
Another python/psycopg question, for which the solution is probably quite simple; I just don't know where to look. I have a query that inserts data originating from an utf-8 encoded XML file....
12
by: Mike Dee | last post by:
A very very basic UTF-8 question that's driving me nuts: If I have this in the beginning of my Python script in Linux: #!/usr/bin/env python # -*- coding: UTF-8 -*- should I - or should I...
38
by: Haines Brown | last post by:
I'm having trouble finding the character entity for the French abbreviation for "number" (capital N followed by a small supercript o, period). My references are not listing it. Where would I...
6
by: jmgonet | last post by:
Hello everybody, I'm having troubles loading a Xml string encoded in UTF-8. If I try this code: ------------------------------ XmlDocument doc=new XmlDocument(); String s="<?xml...
6
by: archana | last post by:
Hi all, can someone tell me difference between unicode and utf 8 or utf 18 and which one is supporting more character set. whic i should use to support character ucs-2. I want to use ucs-2...
7
by: Jimmy Shaw | last post by:
Hi everybody, Is there any SIMPLE way to convert from UTF-16 to UTF-32? I may be mixed up, but is it possible that all UTF-16 "code points" that are 16 bits long appear just the same in UTF-32,...
1
by: sheldon.regular | last post by:
I am new to unicode so please bear with my stupidity. I am doing the following in a Python IDE called Wing with Python 23. äöü äöü '\xc3\xa4\xc3\xb6\xc3\xbc' u'\xe4\xf6\xfc'...
10
by: Jed | last post by:
I have a form that needs to handle international characters withing the UTF-8 character set. I have tried all the recommended strategies for getting utf-8 characters from form input to email...
23
by: Allan Ebdrup | last post by:
I hava an ajax web application where i hvae problems with UTF-8 encoding oc chineese chars. My Ajax webapplication runs in a HTML page that is UTF-8 Encoded. I copy and paste some chineese chars...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.