如何修复 Python 错误“”'ascii' 编解码器无法解码位置 364 中的字节 0xc2:序号不在范围内 (128)”

How to fix Python error "" 'ascii' codec can't decode byte 0xc2 in position 364: ordinal not in range(128)"

我有一个使用 python 的邮件程序,而 运行 我得到

的邮件程序
error "Traceback (most recent call last):
  File "C:\Users\Education\Documents\DDonnelly\Python\Email19_CCS_Renewal\main.py", line 11, in <module>
    mailer()
  File "C:\Users\Education\Documents\DDonnelly\Python\Email19_CCS_Renewal\ccs_renewal_mailer.py", line 35, in mailer
    plaintext_body = ccs_plaintext_message.replace('{first_name}', first_name)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 364: ordinal not in range(128)"

我已将注释“# -- coding: utf-8 --”添加到文件顶部,但我仍然得到此代码。我什至在 CMD 中使用以下命令将注册表默认编码更改为 utf,但运气不佳

"REG ADD HKCU\Console\%SystemRoot^%_system32_cmd.exe /v CodePage /t REG_DWORD /d 65001"

下面是错误引用的代码片段。我真的很感激任何关于如何纠正这个问题并让这个邮件程序运行的指导

代码片段

# -*- coding: utf-8 -*-
import win32com.client as win32
from openpyxl import load_workbook
from ccs_renewal_message import ccs_html_message
from ccs_message_plaintext import ccs_plaintext_message


wb = load_workbook('ccs_renewal_list.xlsx')

获取指定的excelsheet

sheet = wb.get_sheet_by_name('2019 Pending')

def mailer():

    for row in sheet:

        # Stores each individual's information in the variables below
        first_name = row[2].value
        last_name = row[1].value
        full_name = row[2].value + " " + row[1].value
        renewal_date_value = row[4].value
        renewal_date = renewal_date_value.strftime('%m/%d/%Y')
        email = row[3].value
        ncbfaa_id = str(row[0].value)
        # Opens outlook
        outlook = win32.Dispatch('outlook.application')
        # Gets email ready
        mail = outlook.CreateItem(0)

        mail.To = email
        mail.Subject = 'Renewal'

        #Plain Text Message
        plaintext_body = ccs_plaintext_message.replace('{first_name}', first_name)
        mail.Body = plaintext_body.replace('{renewal_date}', renewal_date)

        #HTML Message
        html_body = ccs_html_message.replace('{first_name}', first_name)
        html_body2 = html_body.replace('{id}', ncbfaa_id)
        mail.HTMLBody = html_body2.replace('{renewal_date}', renewal_date)


C:\Users\Education\Documents\DDonnelly\Python\Email19_CCS_Renewal>main.py
Eduardo Lozano's certificate has been created
Traceback (most recent call last):
  File "C:\Users\Education\Documents\DDonnelly\Python\Email19_CCS_Renewal\main.py", line 11, in <module>
    mailer()
  File "C:\Users\Education\Documents\DDonnelly\Python\Email19_CCS_Renewal\ccs_renewal_mailer.py", line 35, in mailer
    plaintext_body = ccs_plaintext_message.replace('{first_name}', first_name)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 364: ordinal not in range(128)
plaintext_body = ccs_plaintext_message.replace('{first_name}', first_name)

在这一行中,您使用 strunicode 转换为编码的 text/bytes。 您可以通过正确解码 first_name 然后传递给替换函数来解决它。

plaintext_body = ccs_plaintext_message.replace('{first_name}', first_name.encode('utf-8'))

您还应该考虑将 ccs_plaintext_message 类型从 str 更改为 unicode。 您只需在 str 前面添加 u 即可。例子

ccs_plaintext_message = u'some string '