473,324 Members | 2,581 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,324 software developers and data experts.

Unicode in MIMEText

Why doesn't this work:

from email.MIMEText import MIMEText
msg = MIMEText(u'\u043a\u0438\u0440\u0438\u043b\u0438\u0 446\u0430')
msg.set_charset('utf-8')
msg.as_string()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.4/email/Message.py", line 129, in as_string
g.flatten(self, unixfrom=unixfrom)
File "/usr/lib/python2.4/email/Generator.py", line 82, in flatten
self._write(msg)
File "/usr/lib/python2.4/email/Generator.py", line 113, in _write
self._dispatch(msg)
File "/usr/lib/python2.4/email/Generator.py", line 139, in _dispatch
meth(msg)
File "/usr/lib/python2.4/email/Generator.py", line 180, in _handle_text
payload = cset.body_encode(payload)
File "/usr/lib/python2.4/email/Charset.py", line 366, in body_encode
return email.base64MIME.body_encode(s)
File "/usr/lib/python2.4/email/base64MIME.py", line 136, in encode
enc = b2a_base64(s[i:i + max_unencoded])
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7:
ordinal not in range(128)

--
damjan
Nov 24 '05 #1
5 4062
> Why doesn't this work:

from email.MIMEText import MIMEText
msg = MIMEText(u'\u043a\u0438\u0440\u0438\u043b\u0438\u0 446\u0430')
msg.set_charset('utf-8')
msg.as_string() .... UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7:
ordinal not in range(128)


It's a real shame that unicode support in the python library is very weak
sometimes...

Anyway I solved my problem by patching email.Charset

--- Charset.py~ 2005-11-24 04:20:09.000000000 +0100
+++ Charset.py 2005-11-24 04:21:02.000000000 +0100
@@ -244,6 +244,8 @@
"""Convert a string from the input_codec to the output_codec."""
if self.input_codec <> self.output_codec:
return unicode(s, self.input_codec).encode(self.output_codec)
+ elif isinstance(s, unicode):
+ return s.encode(self.output_codec)
else:
return s

--
damjan
Nov 24 '05 #2
Damjan wrote:
Why doesn't this work:

from email.MIMEText import MIMEText
msg = MIMEText(u'\u043a\u0438\u0440\u0438\u043b\u0438\u0 446\u0430')
msg.set_charset('utf-8')
msg.as_string()


...
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7:
ordinal not in range(128)

It's a real shame that unicode support in the python library is very weak
sometimes...

Anyway I solved my problem by patching email.Charset

--- Charset.py~ 2005-11-24 04:20:09.000000000 +0100
+++ Charset.py 2005-11-24 04:21:02.000000000 +0100
@@ -244,6 +244,8 @@
"""Convert a string from the input_codec to the output_codec."""
if self.input_codec <> self.output_codec:
return unicode(s, self.input_codec).encode(self.output_codec)
+ elif isinstance(s, unicode):
+ return s.encode(self.output_codec)
else:
return s


.... and being concerned to improve the library you logged this patch in
Sourceforge for consideration by the developers?

That's the only way to guarantee proper consideration of your fix.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Nov 24 '05 #3
> ... and being concerned to improve the library you logged this patch in
Sourceforge for consideration by the developers?

That's the only way to guarantee proper consideration of your fix.


Ok I will, can you confirm that the patch is correct?
Maybe I got something wrong?
--
damjan
Nov 26 '05 #4
Damjan wrote:
... and being concerned to improve the library you logged this patch in
Sourceforge for consideration by the developers?

That's the only way to guarantee proper consideration of your fix.

Ok I will, can you confirm that the patch is correct?
Maybe I got something wrong?

I can't confirm its correctness but I can say it looks reasonable enough
to submit as a path. The fact that you have identified an issue and a
possible fix is quite enough to allow you to submit the patch.

The adequacy of the patch will ultimately be decided by the maintainer
who considers your submission (in all probability Barry Warsaw, but not
necessarily).

Thanks for taking the time to improve the quality of the Python library.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Nov 26 '05 #5
patch submitted...
Thanks for taking the time to improve the quality of the Python library.


Do you think it would be possible to do some kind of an automatic
comprehensive test of compatibility of the standard library with unicode
strings?
--
damjan
Nov 28 '05 #6

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

Similar topics

3
by: Michael Weir | last post by:
I'm sure this is a very simple thing to do, once you know how to do it, but I am having no fun at all trying to write utf-8 strings to a unicode file. Does anyone have a couple of lines of code...
8
by: Bill Eldridge | last post by:
I'm trying to grab a document off the Web and toss it into a MySQL database, but I keep running into the various encoding problems with Unicode (that aren't a problem for me with GB2312, BIG 5,...
4
by: webdev | last post by:
lo all, some of the questions i'll ask below have most certainly been discussed already, i just hope someone's kind enough to answer them again to help me out.. so i started a python 2.3...
2
by: Neil Schemenauer | last post by:
python-dev@python.org.] The PEP has been rewritten based on a suggestion by Guido to change str() rather than adding a new built-in function. Based on my testing, I believe the idea is...
0
by: Robert | last post by:
How do I best guess the right (non-UTF, 8bit) default _charset for MIMEText on an arbitrary computer? Windows default encoding is quasi : 'mbcs' . No information >>>...
24
by: ChaosKCW | last post by:
Hi I am reading from an oracle database using cx_Oracle. I am writing to a SQLite database using apsw. The oracle database is returning utf-8 characters for euopean item names, ie special...
0
by: Dale Strickland-Clark | last post by:
The email module's mimetext handling isn't what you might expect from something that appears to behave like a dictionary. $ python Python 2.5 (r25:51908, May 25 2007, 16:14:04) on linux2 Type...
0
by: Marcus.CM | last post by:
There is a bug with py2exe when (at least under windows) when importing email # example testmime.py import email msg = email.MIMEText.MIMEText("dsafdafdasfA") print "ok" 1. Save the text...
0
by: Marcus.CM | last post by:
Hi, After some debugging, i found the solution is to :- import email import email.mime.text import email.iterators import email.generator import email.utils
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
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...
0
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.