UnicodeEncodeError: 'ascii' codec can't encode character '\u2019' in position 49: ordinal not in range(128)

问题描述 投票:0回答:0

每次尝试将字符串发送到电子邮件时,我都会遇到此错误问题。

UnicodeEncodeError: 'ascii' codec can't encode character '\u2019' in position 49: ordinal not in range(128)

这是我尝试使用 smtplib 发送的字符串:

Headline: The ‘New’ iPhone Actually Isn’t New You might have the impression that the tech world in general is at a bit of a standstill. Companies push new products every year, but tout iterative features rather than revolutionary changes

我尝试使用

.encode('utf-8')
并以这种格式发送电子邮件:

b"Headline: The \xe2\x80\x98New\xe2\x80\x99 iPhone Actually Isn\xe2\x80\x99t New\n\nBrief: ['You might have the impression that the tech world in general is at a bit of a standstill', ' Companies push new products every year, but tout iterative features rather than revolutionary changes']"
data = response.json()["articles"]
    messsage = data[:3]
    three = [f"Headline: {i['title']}\nBrief: {i['description'].split('.')[:2]}" for i in messsage]
    headline = [f"Headline: {i['title']}" for i in messsage]
    brief = [i['description'].split('.')[:2] for i in messsage]

    ha = []
    for i in range(3):
        joined = ".".join(brief[i])
        ha.append(joined)

    for i in range(3):

        with smtplib.SMTP("smtp.gmail.com", 587) as connection:
            connection.starttls()
            connection.login(user=my_email, password=my_passwd)
            connection.sendmail(
                from_addr=my_email,
                to_addrs=my_email,
                msg=f"Subject: Stock Update\n\n{headline[i]}\n{ha[i]}"
            )
Traceback (most recent call last):
  File "C:\Users\Acer\PycharmProjects\100_Days_Of_Python\36\dummy", line 111, in <module>
    get_news()
  File "C:\Users\Acer\PycharmProjects\100_Days_Of_Python\36\dummy", line 54, in get_news
    connection.sendmail(
  File "C:\Users\Acer\AppData\Local\Programs\Python\Python310\lib\smtplib.py", line 875, in sendmail
    msg = _fix_eols(msg).encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode character '\u2018' in position 39: ordinal not in range(128)
python encode
© www.soinside.com 2019 - 2024. All rights reserved.