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

时间:2013-09-02 11:29:13

标签: 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><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()

现在问题是它在Windows 7 x64上运行得很好,但在Debian Linux x64上使用Python 3.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)

琴弦中的德国变音符号正在引起这种情况。但是为什么它在Windows上成功并在Linux上失败?如何使代码与两种环境兼容?我认为,控制台编码似乎与此无关。

2 个答案:

答案 0 :(得分:2)

解决方案是从

更改第7行
msg = MIMEText(emailTextHTML, 'html')

msg = MIMEText(emailTextHTML, 'html', 'utf-8')

现在它适用于两种环境。

Python错误730414380似乎与此有关。所以我的问题更多的是Python 3.2到3.3的问题。

答案 1 :(得分:0)

是的,当你在https://stackoverflow.com/a/18573582/1346705写信时。 Python 3.3包含在MIMEText.__init__()

    # 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'

Python 3.2不包含该特定代码。

应该指定charset(显式或固定,如3.3或某种方式)的原因是有时open等功能使用OS首选编码。这可能使事情复杂化。此外,您的情况下的print可能会导致类似的问题,因为控制台通常不使用Unicode。