[英]UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 UnicodeDecodeError:“ ascii”编解码器无法解码字节0xe3
I am facing this issue while running a mailing script in py 2.7 for the line... 我在py 2.7中为该行运行邮件脚本时遇到了这个问题...
msg.attach(MIMEText(welcome_msg + htmlMessageContent + footer_msg, 'html')) msg.attach(MIMEText(welcome_msg + htmlMessageContent + footer_msg,'html'))
One of the elements of the string you are concatenating 您要连接的字符串的元素之一
welcome_msg + htmlMessageContent + footer_msg
is Unicode, and another of them isn't. 是Unicode,而另一个不是。 When you concatenate the strings Python has convert them all to a common type (Unicode), much as it does when you add an integer to a float.
当您连接字符串时,Python会将它们全部转换为通用类型(Unicode),这与将整数添加到浮点数中的方式很相似。 But the default string conversion to Unicode is ascii, and if the string contains a non-ascii character it will fail.
但是,默认的字符串转换为Unicode是ascii,如果该字符串包含非ascii字符,它将失败。
Find out which string isn't Unicode. 找出哪个字符串不是Unicode。 For this you can use
type()
. 为此,您可以使用
type()
。 Wrap that string in a call to unicode()
that explains how you want '\\xe3'
interpreted. 将该字符串包装到对
unicode()
的调用中,该调用解释了如何解释'\\xe3'
。 For example, if '\\xe3'
should be interpreted as 'ã'
: 例如,如果
'\\xe3'
应解释为'ã'
:
unicode(mystring, encoding='Latin-1')
Then your concatenation should work. 然后,您的串联应该起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.