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

b64encode and unicode problem

Hello everyone,

I am trying to encode a string using b4encode and I get the following
error :
>>b64encode(u"Salut Pierre, comment ça va?")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/base64.py", line 53, in b64encode
encoded = binascii.b2a_base64(s)[:-1]
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in
position 22: ordinal not in range(128)

If I remove the "u" infront of the string, it works. The problem is that
I in my program, the string is given to me un unicode/utf-8. I tried
several things, but I still get it, How can I get it to work, anybody
have any idea?

Thanks,
Gabriel
Jun 27 '08 #1
2 2820
Gabriel Rossetti wrote:
Hello everyone,

I am trying to encode a string using b4encode and I get the following
error :
>>b64encode(u"Salut Pierre, comment ça va?")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/base64.py", line 53, in b64encode
encoded = binascii.b2a_base64(s)[:-1]
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in
position 22: ordinal not in range(128)

If I remove the "u" infront of the string, it works. The problem is that
I in my program, the string is given to me un unicode/utf-8. I tried
several things, but I still get it, How can I get it to work, anybody
have any idea?
>>base64.b64encode(u"Salut Pierre, comment ça va?".encode("utf8"))
'U2FsdXQgUGllcnJlLCBjb21tZW50IMOnYSB2YT8='

unicode is a sequence of codepoints with no predetermined representation as
a byte sequence. Therefore whenever you go from unicode to str you have to
decide upon an encoding. If you don't make that decision python assumes
ascii which will of course fail for non-ascii characters.

Peter

Jun 27 '08 #2
Peter Otten wrote:
Gabriel Rossetti wrote:

>Hello everyone,

I am trying to encode a string using b4encode and I get the following
error :
> >>b64encode(u"Salut Pierre, comment ça va?")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/base64.py", line 53, in b64encode
encoded = binascii.b2a_base64(s)[:-1]
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in
position 22: ordinal not in range(128)

If I remove the "u" infront of the string, it works. The problem is that
I in my program, the string is given to me un unicode/utf-8. I tried
several things, but I still get it, How can I get it to work, anybody
have any idea?

>>>base64.b64encode(u"Salut Pierre, comment ça va?".encode("utf8"))
'U2FsdXQgUGllcnJlLCBjb21tZW50IMOnYSB2YT8='

unicode is a sequence of codepoints with no predetermined representation as
a byte sequence. Therefore whenever you go from unicode to str you have to
decide upon an encoding. If you don't make that decision python assumes
ascii which will of course fail for non-ascii characters.

Peter

Ok, I get it better now, thanks!
Gabriel

Jun 27 '08 #3

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

Similar topics

2
by: Toby Doig | last post by:
When I try to import data from a unicode file into PostgreSQL 7.4 under FreeBSD it appears to now understand the Unicode file format. To demonstrate I export a set of Integers into a Unicode file...
2
by: Priem, Alexander | last post by:
Hi everyone, I have a PostgreSQL 7.4 database running, which was initdb-ed using standard (SQL_ASCII) encoding, with -lc-collate=C option set. Everything is running fine, but I just discovered...
12
by: manstey | last post by:
I am writing a program to translate a list of ascii letters into a different language that requires unicode encoding. This is what I have done so far: 1. I have # -*- coding: UTF-8 -*- as my...
5
by: Norman Diamond | last post by:
Here are two complete lines of output from Visual Studio 2005: 1>$B%W%m%8%'%/%H=PNO$K(B Authenticode $B=pL>$7$F$$$^$9(B... 1>Successfully signed: c:\T The first line means roughly: Doing...
1
by: erikcw | last post by:
Hi, I'm trying to insert some data from an XML file into MySQL. However, while importing one of the files, I got this error: Traceback (most recent call last): File "wa.py", line 304, in ?...
6
by: pabloski | last post by:
Hi to all, I have a little problem with unicode handling under Python. I have this code s = u'A unicode string with this damn apostrophe \x2019' outf = codecs.open('filename.txt', 'w',...
1
by: hugobotha | last post by:
Hi All, I have a problem when saving a text file as a html file in unicode and then google index the Unicode bom as well as I can not open these files. I have a asp page that takes another asp...
0
by: Gabriel Rossetti | last post by:
Max M wrote: Thank you, it's a bit clearer now, I thought unicode and utf8 were synonyms, but from what I understood unicode is sort of like bytecode, and utf8/latin1/etc would be like a asm...
1
by: Victor Lin | last post by:
Hi, I'm writting a application using python standard logging system. I encounter some problem with unicode message passed to logging library. I found that unicode message will be messed up by...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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,...
0
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...
0
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...
0
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 project—planning, coding, testing,...

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.