Python 3.x Python:Windows和Linux之间字符串处理的差异

Python 3.x Python:Windows和Linux之间字符串处理的差异,python-3.x,Python 3.x,我有一个小Python 3代码: # -*- coding: utf-8 -*- import smtplib from email.mime.text import MIMEText emailTextHTML = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><me

我有一个小Python 3代码:

# -*- coding: utf-8 -*-

import smtplib
from email.mime.text import MIMEText

emailTextHTML = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-type" content="text/html;charset=UTF-8"><title>Wöchentliche Ticketbenachrichtigung</title></head><body><p>Hallo ...,</p></body></html>'
msg = MIMEText(emailTextHTML, 'html')
msg['Subject'] = 'TEST Wöchentliche Ticketbenachrichtigung TEST'
msg['From'] = 'reminderscript@blubb.de'
msg['To'] = 'asdf@blubb.de'
s = smtplib.SMTP('192.168.115.99')
#try:
s.send_message(msg)
#except:
print(msg)
s.quit()
#-*-编码:utf-8-*-
导入smtplib
从email.mime.text导入MIMEText
emailTextHTML='Wöchentliche Ticketbenachrichtigung你好…,

' msg=MIMEText(emailTextHTML,'html') msg['Subject']='TEST Wöchentliche ticketbenachrichtighung TEST' msg['From']='reminderscript@blubb.de' msg['To']='asdf@blubb.de' s=smtplib.SMTP('192.168.115.99') #尝试: s、 发送消息(msg) #除: 打印(msg) s、 退出
现在的问题是,它在Windows7x64上使用Python3.3.2运行良好,但在DebianLinuxx64上使用Python3.2.3失败。我在使用上次设置时遇到此错误:

Traceback (most recent call last):
  File "testing.py", line 13, in <module>
    s.send_message(msg)
  File "/usr/lib/python3.2/smtplib.py", line 812, in send_message
    g.flatten(msg_copy, linesep='\r\n')
  File "/usr/lib/python3.2/email/generator.py", line 91, in flatten
    self._write(msg)
  File "/usr/lib/python3.2/email/generator.py", line 137, in _write
    self._dispatch(msg)
  File "/usr/lib/python3.2/email/generator.py", line 163, in _dispatch
    meth(msg)
  File "/usr/lib/python3.2/email/generator.py", line 398, in _handle_text
    super(BytesGenerator,self)._handle_text(msg)
  File "/usr/lib/python3.2/email/generator.py", line 201, in _handle_text
    self.write(payload)
  File "/usr/lib/python3.2/email/generator.py", line 357, in write
    self._fp.write(s.encode('ascii', 'surrogateescape'))
UnicodeEncodeError: 'ascii' codec can't encode character '\xf6' in position 188: ordinal not in range(128)
回溯(最近一次呼叫最后一次):
文件“testing.py”,第13行,在
s、 发送消息(msg)
发送消息中的文件“/usr/lib/python3.2/smtplib.py”,第812行
g、 展平(msg\u copy,linesep='\r\n')
文件“/usr/lib/python3.2/email/generator.py”,第91行,扁平化
自我书写(msg)
文件“/usr/lib/python3.2/email/generator.py”,第137行,在
自动发送(msg)
文件“/usr/lib/python3.2/email/generator.py”,第163行,在
甲基(味精)
文件“/usr/lib/python3.2/email/generator.py”,第398行,文本
超级(字节生成器,自身)。\处理\文本(msg)
文件“/usr/lib/python3.2/email/generator.py”,第201行,文本
自写(有效负载)
写入文件“/usr/lib/python3.2/email/generator.py”,第357行
self._fp.write(s.encode('ascii','subscrateescape'))
UnicodeEncodeError:“ascii”编解码器无法对位置188处的字符“\xf6”进行编码:序号不在范围内(128)

字符串中的德国umlaut导致了这种情况。但为什么它在Windows上成功而在Linux上失败呢?如何使代码与这两种环境兼容?我想,控制台编码似乎与此无关。

解决方案是将第7行从

msg = MIMEText(emailTextHTML, 'html')

现在它可以在两种环境中工作

Python存在bug,并且似乎与此相关。因此,我的问题更多的是Python 3.2到3.3的问题。

是的,正如您在中所写。Python3.3包含在
MIMEText.\uuuu init\uuuuu()

Python 3.2不包含该特定代码

应指定字符集的原因(如3.3中明确或固定,或以某种方式)是,有时
open
等函数使用操作系统首选的编码。这可能使事情复杂化。此外,在您的情况下,
打印
可能会导致类似的问题,因为控制台通常不使用Unicode

msg = MIMEText(emailTextHTML, 'html', 'utf-8')
    # If no _charset was specified, check to see if there are non-ascii
    # characters present. If not, use 'us-ascii', otherwise use utf-8.
    # XXX: This can be removed once #7304 is fixed.
    if _charset is None:
        try:
            _text.encode('us-ascii')
            _charset = 'us-ascii'
        except UnicodeEncodeError:
            _charset = 'utf-8'