473,788 Members | 3,027 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 2319
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
1429
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
2025
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
1582
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. Specifically, a Hex 'BA' is declared as a delimiter. I
1
1488
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 "Unicode" (using Textpad where the options are "utf-8" and "Unicode") and everything was fine. This...
11
3226
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. 'F07JS41C.04389525AA.UPR\xa6INR.E\xa6C-P.D11.081305.P2.KPF.model') which will read as "False" when applying an...
15
2121
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 regular string by now? Is it for historical reasons that we still use ASCII and Latin-1? Why can't...
8
2683
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
2359
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
4149
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 plainTextBytes = enc.GetBytes(plainText)
0
9655
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 usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.