email.Generator.Generator的问题 [英] Problems with email.Generator.Generator

查看:141
本文介绍了email.Generator.Generator的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


下面这段代码给了我一些问题:


来自email.Charset import Charset,QP

来自email.MIMEText导入MIMEText

charset = Charset(''utf-8'')

charset.body_encoding = QP

msg = MIMEText(

你'带有字符的文本需要编码:\ xa3'',

''普通'',



msg.set_charset(charset)

print msg.as_string()


在Python 2.4.2下,这会产生以下输出,正如我所料:

MIME-Version:1.0

Content-Transfer-Encoding:8bit

内容类型:text / plain; charset =" utf-8"


一些需要编码的字符文本:= A3


但是,在Python 2.4.3下,我现在得到:


Traceback(最近一次调用最后一次):

文件" test_encoding.py",第14行,在?

msg.as_string()

文件" c:\ python24 \lib \ email \ Message.py",第129行,

in

as_string

g.flatten(self,unixfrom = unixfrom)

文件" c:\ python24 \lib \ email \ Generator.py",第82行,

in flatten

self._write(msg)

文件" c:\ python24 \ lib\email\Generator.py" ;,第113行,

in _write

self._dispatch(msg)

文件" c :\ python24 \ lib\email \Generator.py",139行,

in

_dispatch

meth(msg)

文件" c:\ python24 \lib \ email \ Generator.py",第182行,

in

_handle_text

self._fp.write(payload)

UnicodeEncodeError:''ascii''编解码器无法编码

character

u''\xa3''位置41:

序数不在范围内(128)


这似乎是由于这种变化:

http://svn.python.org/view/python/br...37910& ; r2 = 42272


....这被称为修复此bug的一部分:

http://sourceforge.net/tracker/?func ... 70& atid = 105470


现在,这是对Generator.py的错误更改还是我做错了什么?


如果是后者,我怎样才能改变我的代码,使其有效我期待吗?


欢呼,


克里斯


-

Simplistix - 内容管理,Zope& Python Consulting

- http://www.simplistix.co.uk

Hi All,

The following piece of code is giving me issues:

from email.Charset import Charset,QP
from email.MIMEText import MIMEText
charset = Charset(''utf-8'')
charset.body_encoding = QP
msg = MIMEText(
u''Some text with chars that need encoding: \xa3'',
''plain'',
)
msg.set_charset(charset)
print msg.as_string()

Under Python 2.4.2, this produces the following output, as I''d expect:

MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset="utf-8"

Some text with chars that need encoding: =A3

However, under Python 2.4.3, I now get:

Traceback (most recent call last):
File "test_encoding.py", line 14, in ?
msg.as_string()
File "c:\python24\lib\email\Message.py", line 129,
in
as_string
g.flatten(self, unixfrom=unixfrom)
File "c:\python24\lib\email\Generator.py", line 82,
in flatten
self._write(msg)
File "c:\python24\lib\email\Generator.py", line 113,
in _write
self._dispatch(msg)
File "c:\python24\lib\email\Generator.py", line 139,
in
_dispatch
meth(msg)
File "c:\python24\lib\email\Generator.py", line 182,
in
_handle_text
self._fp.write(payload)
UnicodeEncodeError: ''ascii'' codec can''t encode
character
u''\xa3'' in position 41:
ordinal not in range(128)

This seems to be as a result of this change:

http://svn.python.org/view/python/br...37910&r2=42272

....which is referred to as part of a fix for this bug:

http://sourceforge.net/tracker/?func...70&atid=105470

Now, is this change to Generator.py in error or am I doing something wrong?

If the latter, how can I change my code such that it works as I''d expect?

cheers,

Chris

--
Simplistix - Content Management, Zope & Python Consulting
- http://www.simplistix.co.uk

推荐答案

Chris Withers ha scritto:
Chris Withers ha scritto:

Hi All,


下面这段代码给了我一些问题:


来自email.Charset进口Charset,QP

来自email.MIMEText导入MIMEText

charset = Charset(''utf-8'')

charset.body_encoding = QP

msg = MIMEText(

你用一些需要编码的字符文本:\ xa3'',

''普通'',



msg.set_charset(charset)

print msg.as_string()

在Python 2.4.2下,这会产生以下输出,正如我所料:
Hi All,

The following piece of code is giving me issues:

from email.Charset import Charset,QP
from email.MIMEText import MIMEText
charset = Charset(''utf-8'')
charset.body_encoding = QP
msg = MIMEText(
u''Some text with chars that need encoding: \xa3'',
''plain'',
)
msg.set_charset(charset)
print msg.as_string()

Under Python 2.4.2, this produces the following output, as I''d expect:


[...]

然而,在Python 2.4.3下,我现在get:
[...]
However, under Python 2.4.3, I now get:



试试使用:


msg = MIMEText(

u'有些需要编码的字符文本:\ xa3'',

_charset =''utf-8'',



您将获得错误:

Traceback(最近一次调用最后一次) :

文件"< pyshell#4>",第3行,在-toplevel-

_charset =''utf-8'',

文件C:\Python2.4 \lib \ email \ MIMEText.py,第28行,在__init__

self.set_payload(_text,_charset)

文件C:\Python2.4 \lib \ email \ Message.py,第218行,在set_payload中

self.set_charset(charset)

文件" C:\Python2.4 \lib \ email \ Message.py",第260行,在set_charset中

self._payload = charset.body_encode( self._payload)

文件" C:\Python2.4 \ lib \ email \ Charset.py",第366行,在body_encode中

返回电子邮件.base64MIME.body_encode(s)

Fi leC:\Python2.4 \lib \ email \ base64MIME.py,第136行,编码

enc = b2a_base64(s [i:i + max_unencoded])

UnicodeEncodeError:''ascii''编解码器不能编码字符u''\ xa3''

位置41:序数不在范围内(128)



问候Manlio Perillo

Try with:

msg = MIMEText(
u''Some text with chars that need encoding: \xa3'',
_charset=''utf-8'',
)
and you will obtain the error:
Traceback (most recent call last):
File "<pyshell#4>", line 3, in -toplevel-
_charset=''utf-8'',
File "C:\Python2.4\lib\email\MIMEText.py", line 28, in __init__
self.set_payload(_text, _charset)
File "C:\Python2.4\lib\email\Message.py", line 218, in set_payload
self.set_charset(charset)
File "C:\Python2.4\lib\email\Message.py", line 260, in set_charset
self._payload = charset.body_encode(self._payload)
File "C:\Python2.4\lib\email\Charset.py", line 366, in body_encode
return email.base64MIME.body_encode(s)
File "C:\Python2.4\lib\email\base64MIME.py", line 136, in encode
enc = b2a_base64(s[i:i + max_unencoded])
UnicodeEncodeError: ''ascii'' codec can''t encode character u''\xa3'' in
position 41: ordinal not in range(128)


Regards Manlio Perillo


Manlio Perillo写道:
Manlio Perillo wrote:

尝试使用:


msg = MIMEText(

你'带有需要编码的字符的文本:\ xa3'',

_charset =''utf-8'',




你会得到错误:


Traceback(最近一次调用最后一次):

文件"< pyshell#4>",第3行,在-toplevel-

_charset =' 'utf-8'',

文件C:\Python2.4 \lib \ email \ MIMEText.py,第28行,在__init__

self.set_payload(_t ext,_charset)

文件C:\Python2.4 \lib \ email \ Message.py,第218行,在set_payload

self。 set_charset(charset)

文件" C:\Python2.4 \lib \ email \ Message.py",第260行,在set_charset中

self。 _payload = charset.body_encode(self._payload)

文件" C:\Python2.4 \ lib \ email \ Charset.py",第366行,在body_encode中

返回email.base64MIME.body_encode

文件C:\Python2.4 \lib \ email \ base64MIME.py,第136行,编码

enc = b2a_base64(s [i:i + max_unencoded])

UnicodeEncodeError:''ascii''编解码器无法编码字符u''\ x3'''在

位置41:序数不在范围内(128)
Try with:

msg = MIMEText(
u''Some text with chars that need encoding: \xa3'',
_charset=''utf-8'',
)
and you will obtain the error:

Traceback (most recent call last):
File "<pyshell#4>", line 3, in -toplevel-
_charset=''utf-8'',
File "C:\Python2.4\lib\email\MIMEText.py", line 28, in __init__
self.set_payload(_text, _charset)
File "C:\Python2.4\lib\email\Message.py", line 218, in set_payload
self.set_charset(charset)
File "C:\Python2.4\lib\email\Message.py", line 260, in set_charset
self._payload = charset.body_encode(self._payload)
File "C:\Python2.4\lib\email\Charset.py", line 366, in body_encode
return email.base64MIME.body_encode(s)
File "C:\Python2.4\lib\email\base64MIME.py", line 136, in encode
enc = b2a_base64(s[i:i + max_unencoded])
UnicodeEncodeError: ''ascii'' codec can''t encode character u''\xa3'' in
position 41: ordinal not in range(128)



好​​的,但我没看到如何用另一个替换一个unicode错误

任何帮助...:-S


克里斯


-

Simplistix - 内容管理,Zope& Python Consulting

- http://www.simplistix.co.uk


Chris Withers写道:
Chris Withers wrote:

下面这段代码给了我一些问题:<来自email.Charset进口Charset,QP

来自email.MIMEText导入MIMEText

charset = Charset(''utf-8 '')

charset.body_encoding = QP

msg = MIMEText(

''普通'',



msg.set_charset(charset)

打印消息.as_string()

在Python 2.4.2下,这会产生以下输出,正如我所料:


MIME-Version :1.0

内容传输编码:8位

内容类型:text / plain; charset =" utf-8"


一些需要编码的字符文本:= A3


但是,在Python 2.4.3下,我现在得到:


Traceback(最近一次调用最后一次):

文件" test_encoding.py",第14行,在?

msg.as_string()

文件" c:\ python24 \lib \ email \ Message.py",第129行,

in

as_string

g.flatten(self,unixfrom = unixfrom)

文件" c:\ python24 \lib \ email \ Generator.py",第82行,

in flatten

self._write(msg)

文件" c:\ python24 \ lib\email\Generator.py" ;,第113行,

in _write

self._dispatch(msg)

文件" c :\ python24 \ lib\email \Generator.py",139行,

in

_dispatch

meth(msg)

文件" c:\ python24 \lib \ email \ \\ GENnerator.py",第182行,

in

_handle_text

self._fp.write(有效载荷)

UnicodeEncodeError:''ascii''编解码器无法编码

字符

u''\xa3''位置41:

序数不在范围内(128)


这似乎是由于这种变化:

The following piece of code is giving me issues:

from email.Charset import Charset,QP
from email.MIMEText import MIMEText
charset = Charset(''utf-8'')
charset.body_encoding = QP
msg = MIMEText(
u''Some text with chars that need encoding: \xa3'',
''plain'',
)
msg.set_charset(charset)
print msg.as_string()

Under Python 2.4.2, this produces the following output, as I''d expect:

MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset="utf-8"

Some text with chars that need encoding: =A3

However, under Python 2.4.3, I now get:

Traceback (most recent call last):
File "test_encoding.py", line 14, in ?
msg.as_string()
File "c:\python24\lib\email\Message.py", line 129,
in
as_string
g.flatten(self, unixfrom=unixfrom)
File "c:\python24\lib\email\Generator.py", line 82,
in flatten
self._write(msg)
File "c:\python24\lib\email\Generator.py", line 113,
in _write
self._dispatch(msg)
File "c:\python24\lib\email\Generator.py", line 139,
in
_dispatch
meth(msg)
File "c:\python24\lib\email\Generator.py", line 182,
in
_handle_text
self._fp.write(payload)
UnicodeEncodeError: ''ascii'' codec can''t encode
character
u''\xa3'' in position 41:
ordinal not in range(128)

This seems to be as a result of this change:

http ://svn.python.org/view/python/br...37910& r2 = 42272


>

.. .which被称为修复此bug的一部分:

>
...which is referred to as part of a fix for this bug:

http://sourceforge.net/tracker/?func...70&atid=105470


>

N. ow,这是对Generator.py的错误更改还是我在做什么

错了?
>
Now, is this change to Generator.py in error or am I doing something
wrong?



我不熟悉电子邮件包来回答这个问题。

I''m not familiar enough with the email package to answer that.


如果是后者,我怎么能改变我的代码,使它像我期望的那样工作?
If the latter, how can I change my code such that it works as I''d expect?



email.Generator和email.Message在内部使用cStringIO.StringIO,其中
无法处理unicode。快速修复可能是猴子补丁:


来自StringIO import StringIO

来自电子邮件导入生成器,消息

生成器。 StringIO = Message.StringIO = StringIO

#您的代码在这里


Peter

email.Generator and email.Message use cStringIO.StringIO internally, which
can''t cope with unicode. A quick fix might be to monkey-patch:

from StringIO import StringIO
from email import Generator, Message
Generator.StringIO = Message.StringIO = StringIO
# your code here

Peter


这篇关于email.Generator.Generator的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆