【问题标题】:Encoding ascii symbol within html在html中编码ascii符号
【发布时间】:2018-06-11 12:51:17
【问题描述】:

我正在通过电子邮件发送 html,html 的某些行应如下所示:

Monday 11/06 3pm W - 267°

有很多行使用该符号,它们位于 html 上的不同标签中,例如:

<p> results are the following:
    <ul>
        <li> Monday 11/06 3pm W - 267°
        <li> Tuesday 12/06 3pm W - 279°
    </ul>
 </p>

对于我使用的编码:

html = MIMEText(html, 'html')
msg.attach(html)

这样我的标签将被编码为 html 标签。 我已经意识到为了发送度数符号,我需要将 html 编码为 ascii' 但是结果是将我的整个 html 作为纯文本发送。

主要问题是我使用的符号不在 html 的末尾或开头,所以我不确定如何正确附加它。

如果我尝试将 ascii 符号作为文本发送,我会收到以下异常:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xb0' in position 617: ordinal not in range(128)

非常感谢帮助者。

【问题讨论】:

  • 哪个版本的 Python 以及html 值的 str 编码(即,它是如何获得的?)将很重要。

标签: python html ascii


【解决方案1】:

直接以 html 格式发送消息:

text = '''
<p> results are the following:
    <ul>
        <li> Monday 11/06 3pm W - 267°</li>
        <li> Tuesday 12/06 3pm W - 279°</li>
    </ul>
 </p>
'''
msg = MIMEText(text, 'html')

【讨论】:

  • 这就是我首先提出这个问题的原因...UnicodeEncodeError: 'ascii' codec can't encode character u'\xb0' in position 617: ordinal not in range(128) @Goran
猜你喜欢
  • 1970-01-01
  • 2013-02-28
  • 2021-11-17
  • 1970-01-01
  • 1970-01-01
  • 2016-11-16
  • 2016-01-25
  • 2012-04-07
  • 2012-01-15
相关资源
最近更新 更多