473,386 Members | 1,621 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,386 software developers and data experts.

Questions about working with character encodings

I am going to demonstrate my complete lack of understanding as to
going back and forth between
character encodings, so I hope someone out there can shed some light
on this. I have always
depended on the kindness of strangers... :-)

I'm playing around with some very simplistic french to english
translation. As some text to
work with, I copied the following from a french news site:

Dans les années 1960, plus d'une voiture sur deux vendues aux
Etats-Unis était fabriquée par GM.
Pendant que les ventes s'effondrent, les pertes se creusent :
sur les neuf premiers mois de l'année 2005,
elles s'élèvent à 3,8 milliards de dollars (3,18 milliards
d'euros), et le dernier trimestre s'annonce difficile.
Quant à la dette, elle est hors normes : 285 milliards de
dollars, soit une fois et demie le chiffre d'affaires.
GM est désormais considéré par les agences de notation
financière comme un investissement spéculatif.
Un comble pour un leader mondial !

Of course, it has lots of accented, non-ascii characters. However, it
posted just fine into both
this email program (hopefully it displays equally well at the other
end), and into my Python
editing program (jEdit).

To start with, I'm not at all cognizant of how either the editor or
the mail program could even
know what encodings to use to display this text properly...

Next, having got the text into the Python file, I presumably have to
encode it as a Unicode
string, but trying something like text = u"""désormais considéré"""
complains to the effect
that :

UnicodeEncodeError: 'ascii' codec can't encode character u'\x8e'
in position 13: ordinal not in range(128)

This occurs even with the first line in the file of

# -*- coding: latin-1 -*-

which I'd hoped would include what I think of as the latin characters
including all those ones with
graves, agues, circonflexes, umlauts, cedilles, and so forth.
Apparently it does not :-)

So I really have two questions:

1) How the heck did jEdit understand the text with all the accents
I pasted into it? More
specifically, how did it know the proper encoding to use?

2) How do I get Python to understand this text? Is there some sort
of coding that will
work in almost every circumstance?

Many thanks for your patience with someone completely new to this
aspect of text handling,

Ken
Dec 15 '05 #1
1 2436
Kenneth McDonald <ke****************@sbcglobal.net> wrote:
I am going to demonstrate my complete lack of understanding as to
going back and forth between
character encodings, so I hope someone out there can shed some light
on this. I have always
depended on the kindness of strangers... :-)

I'm playing around with some very simplistic french to english
translation. As some text to
work with, I copied the following from a french news site:

Dans les années 1960, plus d'une voiture sur deux vendues aux
Etats-Unis était fabriquée par GM.
Pendant que les ventes s'effondrent, les pertes se creusent :
sur les neuf premiers mois de l'année 2005,
elles s'élèvent Ã* 3,8 milliards de dollars (3,18 milliards
d'euros), et le dernier trimestre s'annonce difficile.
Quant Ã* la dette, elle est hors normes : 285 milliards de
dollars, soit une fois et demie le chiffre d'affaires.
GM est désormais considéré par les agences de notation
financière comme un investissement spéculatif.
Un comble pour un leader mondial !

Of course, it has lots of accented, non-ascii characters. However, it
posted just fine into both
this email program (hopefully it displays equally well at the other
end),
It has correct charset header indicating ISO-8859-1 encoding, so yes, it
displayed correctly.
and into my Python
editing program (jEdit).

To start with, I'm not at all cognizant of how either the editor or
the mail program could even
know what encodings to use to display this text properly...
You did not tell us what OS are you using, but in case of Unix, it all
goes up and down with locale - you can transparently pass around text
data as long as the characters are in the repertoire of your locale - of
course, as long as the applications are locale-aware - many older ones
are not. (It is best to use UTF-8 encoding, so that all the more or less
obscure characters can be represented)

If you have Windows, it depends on programs working with old 8-bit ANSI
API, or new unicode API. If the programs use unicode API, you can
without problems pass data around, if they use 8-bit API, you are
restricted to the characters from your system codepage.

Next, having got the text into the Python file, I presumably have to
encode it as a Unicode
string, but trying something like text = u"""désormais considéré"""
complains to the effect
that :

UnicodeEncodeError: 'ascii' codec can't encode character u'\x8e'
in position 13: ordinal not in range(128)

This occurs even with the first line in the file of

# -*- coding: latin-1 -*-

which I'd hoped would include what I think of as the latin characters
including all those ones with
graves, agues, circonflexes, umlauts, cedilles, and so forth.
latin-1 is not enough for proper French (lack of Å“). It is not even
enough for English, it lacks proper typographic quotes and so on.
Apparently it does not :-)
Well, it would be enough for your example, "désormais considéré"
does indeed fit into latin-1. But python complains about character \x8e,
which indeed does not belong to latin-1. Without knowing your OS and
your locale (or ANSI codepage), we cannot tell how it got there.

So I really have two questions:

1) How the heck did jEdit understand the text with all the accents
I pasted into it? More
specifically, how did it know the proper encoding to use?
jEdit is written in Java, right? Java has a good internal unicode
support, so if your OS allowed it, pasting from WWW browser worked since
the browser had to new the encoding (in order to display it properly).

2) How do I get Python to understand this text? Is there some sort
of coding that will
work in almost every circumstance?


utf-8, obviously. Unless you have a strong reason not to do so, use
utf-8 exclusively - you never know what strange character can appear
(even in plain English), and you working and tested application will
start crashing when it gets to the real worls.

So, use # -*- coding: utf-8 -*-, but MAKE SURE jEdit is configured to
save the file in utf-8 encoding (not knowing jEdit, I cannot tell you
how to achieve this, but jEdit's www page claims that jEdit does support
utf-8).

Then there is a little problem with python stdout trying to convert
unicode strings into system default encoding and failing if it cannot be
done, but let's leave this for the moment :-)

--
-----------------------------------------------------------
| Radovan GarabÃ*k http://kassiopeia.juls.savba.sk/~garabik/ |
| __..--^^^--..__ garabik @ kassiopeia.juls.savba.sk |
-----------------------------------------------------------
Antivirus alert: file .signature infected by signature virus.
Hi! I'm a signature virus! Copy me into your signature file to help me spread!
Dec 15 '05 #2

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

Similar topics

4
by: Fuzzyman | last post by:
I have a couple of questions about the UTF encodings. The codecs module has constants definded for the UTF32 encoding, yet this encoding isn't supported as a standard encoding. Why isn't it...
18
by: SwordAngel | last post by:
Hello, I'm looking for a program that converts characters of different encodings (such as EUC-JP, Big5, GB-18030, etc.) into HTML ampersand escape sequences. Anybody knows where I can find one? ...
4
by: HeroOfSpielburg | last post by:
Hello, I am trying to using the Shift_JIS character set in my web pages, and have specified it as such in the <head>. <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS"> ...
37
by: chandy | last post by:
Hi, I have an Html document that declares that it uses the utf-8 character set. As this document is editable via a web interface I need to make sure than high-ascii characters that may be...
38
by: Luke Matuszewski | last post by:
Welcome I have read the in the faq from jibbering about the generic DynWrite, but i also realized that is uses only innerHTML feature of HTML objects. (1) Is there a DOM function which is very...
13
by: Michal | last post by:
Hello, is there any way how to detect string encoding in Python? I need to proccess several files. Each of them could be encoded in different charset (iso-8859-2, cp1250, etc). I want to detect...
37
by: Zhiv Kurilka | last post by:
Hi, I have a text file with following content: "((^)|(.* +))§§§§§§§§" if I read it with: k=System.IO.StreamReader( "file.txt",System.Text.Encoding.ASCII); k.readtotheend()
9
by: Alex Shao | last post by:
Dear all, I have a questions about whether it is possible to use UNICODE by Standard C++. If not, Are there any libraries can be used to achieve it. Any help will be appreciated.
17
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, Wide character and multi-byte character are two popular encoding schemes on Windows. And wide character is using unicode encoding scheme. But each time I feel confused when...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.