AttributeError:编码

时间:2014-09-17 13:10:34

标签: python python-2.7

当我通过漂亮的表

时,我收到以下错误
    msg = MIMEText(self.body)
  File "/usr/lib64/python2.6/email/mime/text.py", line 30, in __init__
    self.set_payload(_text, _charset)
  File "/usr/lib64/python2.6/email/message.py", line 224, in set_payload
    self.set_charset(charset)
  File "/usr/lib64/python2.6/email/message.py", line 266, in set_charset
    cte(self)
  File "/usr/lib64/python2.6/email/encoders.py", line 73, in encode_7or8bit
    orig.encode('ascii')
  File "/usr/lib/python2.6/site-packages/prettytable.py", line 217, in __getattr__
    raise AttributeError(name)
AttributeError: encode

2 个答案:

答案 0 :(得分:1)

在将表格添加到电子邮件之前,先将表格显式转换为unicode字符串对象:

yourtable = unicode(yourtable)

答案 1 :(得分:0)

甚至可以使用:

from django.utils.encoding import force_text
msg = MIMEText(force_text(self.body))

但请注意,因为它似乎是自我。身体不是一个字符串......?