473,884 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is there a way to change the default string encoding?

Is there a way to change the default string encoding used by the
string.encode() method? My default environment is utf-8 but I need it
to be latin-1 to avoid errors like this:
>>'Andr\xe9 Ramel'.decode()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeEr ror: 'ascii' codec can't decode byte 0xe9 in position 4:
ordinal not in range(128)

I can't change the code to pass an encoding argument to the decode
method because it's someone else's code.

Thanks,
rg
Aug 21 '07 #1
4 2590
Ron Garret wrote:
Is there a way to change the default string encoding used by the
string.encode() method?
encode() or decode()? Encoding is best handled by the output stream, e. g.
passing codecs.open(... ) instead of the builtin open(...).

My default environment is utf-8 but I need it
to be latin-1 to avoid errors like this:
>>>'Andr\xe9 Ramel'.decode()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeEr ror: 'ascii' codec can't decode byte 0xe9 in position 4:
ordinal not in range(128)
If your environment were latin-1, you'd get the same error because Python
assumes ascii by default.
I can't change the code to pass an encoding argument to the decode
method because it's someone else's code.
Does that code accept unicode strings? Try to pass u"Andre\xe9 Ramel"
instead of the byte string.

If all else fails there's
>>sys.setdefaul tencoding("lati n1")
"Andre\xe9 Ramel".decode()
u'Andre\xe9 Ramel'

but that's an evil hack, you should rather talk to the maintainer of the
offending code to update it to accept unicode.

Peter
Aug 21 '07 #2
In article <fa************ *@news.t-online.com>,
Peter Otten <__*******@web. dewrote:
If all else fails there's
>sys.setdefault encoding("latin 1")
"Andre\xe9 Ramel".decode()
u'Andre\xe9 Ramel'

but that's an evil hack, you should rather talk to the maintainer of the
offending code to update it to accept unicode.
Yes, but I need to hack around it until I can get it fixed.

Thanks!

rg
Aug 21 '07 #3
Il Mon, 20 Aug 2007 18:44:39 -0700, Ron Garret ha scritto:
Is there a way to change the default string encoding ...
Dive Into Python. Section 9 on http://diveintopython.org/xml_processing/
unicode.html

That will help.

Bye
Fabio
Aug 21 '07 #4
Ron Garret wrote:
In article <fa************ *@news.t-online.com>,
Peter Otten <__*******@web. dewrote:
>If all else fails there's
>>sys.setdefaul tencoding("lati n1")
"Andre\xe9 Ramel".decode()
u'Andre\xe9 Ramel'

but that's an evil hack, you should rather talk to the maintainer of the
offending code to update it to accept unicode.

Yes, but I need to hack around it until I can get it fixed.
Oops, the snippet above omits the actual hack. It should be
>>import sys
reload(sys)
<module 'sys' (built-in)>
>>sys.setdefaul tencoding("lati n1")
"Andre\xe9 Ramel".decode()
u'Andre\xe9 Ramel'

Peter
Aug 21 '07 #5

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

Similar topics

2
49387
by: Ann | last post by:
Hi, Is there any way to Change encoding of Java Vm to ISO-8859-1? i am using Java vm along with an application called opencms. I get the following error message.. Error: the encoding of your Java VM is different from the OpenCms encoding! Java VM file encoding: UTF-8
5
3676
by: Hardy Wang | last post by:
Hi, I have following code: Encoding mode; // Encoding.Default or Encoding.UTF8 FileStream sb = new FileStream(fullPathAndFileName, FileMode.Create); StreamWriter sw = new StreamWriter(sb, mode); sw.Write(textContent); sw.Close(); My question is under what situation, the saved files are different by calling Encoding.Default and Encoding.UTF8.
0
1905
by: Brad Wood | last post by:
I have a web service that returns an xml doc as a string. I use a StringWriter with my XmlTextWriter and the encoding attribute for the document becomes utf-16. If I save the result of the web service to a file, the document won't parse in I.E. unless I change the responseEncoding in my web.config to utf-16 and write the result to file encoded in unicode. I've read that utf-8 is a more standard encoding; is it acceptable to change the...
0
493
by: 6kjfsyg02 | last post by:
I have written a client to a web service. I use ASP.NET 1.1 for the client. It worked until I tried to send accented characters. Then the service answered that my signature is not valid. I was told by the people running the service that their host takes ISO-8859-1 (Latin1) and nothing else. I had been sending the default UTF-8. I tried to send Latin1 with this: RequestEncoding= System.Text.Encoding.GetEncoding( "ISO-8859-1");
7
5796
by: MrNobody | last post by:
I am having alot of difficulty with text files in .NET when they have special characters like í, ó, ç etc... When i read a text file with them and then write it back out it ignores all of those characters completely. I tried all the encoding types and it seems only Encoding.Default does it right... but it sounds dangerous... can I rely on Encoding.Default behaving like this for all other machines?
14
5789
by: Zoro | last post by:
My task is to read html files from disk and save them onto SQL Server database field. I have created an nvarchar(max) field to hold them. The problem is that some characters, particularly html entities, and French/German special characters are lost and/or replaced by a question mark. This is really frustrating. I have tried using StreamReader with ALL the encodings available and none work correctly. Each encoding handles some characters...
11
3604
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function Read_bin(ByVal ruta As String) Dim cadena As String = "" Dim dato As Array If File.Exists(ruta) = True Then
11
26665
by: cybervigilante | last post by:
I can't seem to change the include path on my local winmachine no matter what I do. It comes up as includ_path .;C:\php5\pear in phpinfo() but there is no such file. I installed the WAMP package and PEAR is in c:\wamp\php\pear I modified php.ini in the c:\wamp\php directory to reflect the actual path, but even stopping and restarting my server shows the c: \php5\pear path. I can't change it no matter what I do I also tried the...
1
2107
by: Macneed | last post by:
I want to write a code to determine the homepage content was change, but if the homepage is write by asp or something else, (e.g. livescore.com here) i can't get the real homepage content by the following code, what can i do? if it is not related to c# which newsgroup should i post to? using System; using System.Net;
0
9953
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
9799
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11167
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10768
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
10868
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
10422
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5808
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...
0
6009
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4231
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.