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 2 2815
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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 ?...
|
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',...
|
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...
|
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...
|
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...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |