473,549 Members | 2,209 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Encoding confusion, please help

Hi,

Probing my system from Python 2.3.4 gives
import locale, sys
locale.getdefau ltlocale() ('fi_FI', 'cp1252') locale.getprefe rredencoding() 'cp1252' sys.getdefaulte ncoding()

'iso-8859-1'

Manual says:

locale.getprefe rredencoding():
Return the encoding used for text data, according to user preferences...

sys.getdefaulte ncoding()
Return the name of the current default string encoding
used by the Unicode implementation

When should I use locale.getprefe rredencoding() and when
sys.getdefaulte ncoding()?

Why two different encodings 'cp1252' and 'iso-8859-1' are provided
for my Windows 2000 system?

-pekka-

Jul 18 '05 #1
3 2309
Pekka Niiranen wrote:
>>> sys.getdefaulte ncoding()
'iso-8859-1'


This is already troublesome; it means somebody (perhaps you)
has tampered with your Python installation. The default system
encoding is ascii, and it should not be changed unless
absolutely necessary.
When should I use locale.getprefe rredencoding() and when
sys.getdefaulte ncoding()?
There should never be a need to probe sys.getdefaulte ncoding(),
as it should always be ascii.

locale.getprefe rredencoding() should be used when converting
Unicode strings to and from byte strings to be stored on the local
system (e.g. in files). Notice that this may or may not be adequate
also when printing data to the terminal. Specifically, on Windows,
the terminal often uses yet another encoding.
Why two different encodings 'cp1252' and 'iso-8859-1' are provided
for my Windows 2000 system?


Python provides many more encodings, including UTF-8, KOI-8R,
ISO-8859-2, cp1250, and so on. Having many codecs available in
the library is a good thing, because different applications have
different needs.

I somehow feel this doesn't answer your question, but then, I don't
fully understand the question.

Regards,
Martin
Jul 18 '05 #2
Martin v. Lwis wrote:
Pekka Niiranen wrote:
>>> sys.getdefaulte ncoding() 'iso-8859-1'

This is already troublesome; it means somebody (perhaps you)
has tampered with your Python installation. The default system
encoding is ascii, and it should not be changed unless
absolutely necessary.


I do not recall changing it manually so just in case I reinstalled
latest versions of my default set of python tools in this order:

Python-2.3.4.exe
pywin32-203.win32-py2.3.exe
wxPython2.5-win32-unicode-2.5.3.1-py23.exe
ctypes-0.9.2.win32-py2.3.exe
numarray-1.1.win32-py2.3.exe
pychecker-0.8.14

and now it IS "ascii".
When should I use locale.getprefe rredencoding() and when
sys.getdefaulte ncoding()?

There should never be a need to probe sys.getdefaulte ncoding(),
as it should always be ascii.

locale.getprefe rredencoding() should be used when converting
Unicode strings to and from byte strings to be stored on the local
system (e.g. in files). Notice that this may or may not be adequate
also when printing data to the terminal. Specifically, on Windows,
the terminal often uses yet another encoding.


Can I find out the terminal encoding somehow?
Why two different encodings 'cp1252' and 'iso-8859-1' are provided
for my Windows 2000 system?

Python provides many more encodings, including UTF-8, KOI-8R,
ISO-8859-2, cp1250, and so on. Having many codecs available in
the library is a good thing, because different applications have
different needs.

I somehow feel this doesn't answer your question, but then, I don't
fully understand the question.


The reason I asked was that since my Window's regional settings
matches "cp1252" I was puzzled by sys.getdefaulte ncoding()
not being the same.
Regards,
Martin

Jul 18 '05 #3
Pekka Niiranen wrote:
Can I find out the terminal encoding somehow?


On Windows:

D:\>help chcp
Displays or sets the active code page number.

CHCP [nnn]

nnn Specifies a code page number.

Type CHCP without a parameter to display the active code page number.

D:\>chcp
Active code page: 437

Kent
Jul 18 '05 #4

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

Similar topics

8
1423
by: Edward K. Ream | last post by:
The documentation for encoding lines at C:\Python23\Doc\Python-Docs-2.3.1\whatsnew\section-encodings.html states: "Encodings are declared by including a specially formatted comment in the first or second line of the source file." In fact, contrary to the implication, the Python 2.3 parser does not look
1
2012
by: news.hunterlink.net.au | last post by:
(* note the escaped ampersand and the character reference have extra spaces to avoid being converted when viewed) I have a job that requires the following <ThisElement>Here is some text & a m p ; here is a & # x E 2 ; character</ThisElement> to end up as
6
1563
by: quincy | last post by:
I need help. Please bear with this. I have a program. It takes in files that are delimited. The delimiters are declared in the file by looking at fixed positions in the file (If you work with ANSI x12 files, you know what I mean). This normally isn't a problem, but I'm getting a file that is using some odd characters as delimiters. ...
1
1478
by: Brad Wood | last post by:
I am testing a web service that returns an XML doc as a string. I went through troubles when developing it whereby the saved output of the service would not display in Internet Explorer due to encoding confusion. I finally settled on setting responseEncoding in my web.config file to "utf-16" and saving the output of the service encoded as...
11
3210
by: utabintarbo | last post by:
Here is my situation: I am trying to programatically access files created on an IBM AIX system, stored on a Sun OS 5.8 fileserver, through a samba-mapped drive on a Win32 system. Not confused? OK, let's move on... ;-) When I ask for an os.listdir() of a relevant directory, I get filenames with embedded escaped characters (ex....
15
2084
by: John Salerno | last post by:
Forgive my newbieness, but I don't quite understand why Unicode is still something that needs special treatment in Python (and perhaps elsewhere). I'm reading Dive Into Python right now, and it constantly refers to a 'regular string' versus a 'Unicode string' and how you need to convert back and forth. But why isn't Unicode considered a...
8
2670
by: Erwin Moller | last post by:
Hi group, I could use a bit of guidance on the following matter. I am starting a new project now and must make some decisions regarding encoding. Environment: PHP4.3, Postgres7.4.3 I must be able to receive forminformation and store that in a database and later produce it on screen on the client (just plain HTML).
2
2346
by: =?Utf-8?B?bW9pdGFs?= | last post by:
I have a client application ( web service ) write in vs2005 consuming an external web service with WSE 2.0 (x509). The response is in hebrew ( windows-1255 ). In the Trace files I see the hebrew fine, but the response is gibrish. Did any one know how to change the encoding of the response. -- moital
9
4123
by: =?Utf-8?B?RGFu?= | last post by:
I have the following code section that I thought would strip out all the non-ascii characters from a string after decoding it. Unfortunately the non-ascii characters are still in the string. What am I doing wrong? Dim plainText As String plainText = "t═e" Dim plainTextBytes() As Byte Dim enc As Encoding = Encoding.ASCII...
0
7548
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7472
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7743
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7504
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5391
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3499
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1083
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
786
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.