繁体   English   中英

UnicodeEncodeError: 'ascii' 编解码器无法编码字符 '\’'

[英]UnicodeEncodeError: 'ascii' codec can't encode character '\u2019'

当我尝试将 Twitter 推文发送到电子邮件时收到一条错误消息。 错误信息是:

UnicodeEncodeError: 'ascii' 编解码器无法对位置 114 中的字符 '\’' 进行编码:序号不在范围内 (128)

我的代码如下:


# Python 3.9

import smtplib, requests
import datetime as datetime

# twitter api function (not shown) retrieves a series of tweets - these are added to a dictionary (below)


tweets = {'climate change': 'Eco1stArt - USATeam Animal rides to the rescue in nine-year-old Zac’s latest book about the climate change emergency #books -  twitter short URL  \ncaykahvekeyfi - istanbultwitter short URL\nMaura2e2_ - Killaloe Co. Clare🇮🇪Éire🇮🇪Politicians refusing to treat the #ClimateEmergency as an emergency are committing crimes against humanity.twitter short URL\n"}

# I cant show the actual twitter short URLs in teh tweet data - 
# code to convert the dictionary into a formatted string

tweeter = ""
for k, v in tweets.items():
    tweeter += f"\n{k}\n"
    tweeter += f"{v}"

# code to build and send the email

subject = "Todays Tweets.."
email_address = "miles-s@hotmail.co.uk"

curr_date = get_date()
my_email = "email@email.com"
PW = "mypassword"
Gmail = "smtp.service.com"
with smtplib.SMTP("smtp.service.com", port=587) as connection:
    connection.starttls() # decures the email message
    connection.login(user=my_email, password = PW)
    connection.sendmail(
        from_addr=my_email,
        to_addrs=email_address,
        msg=f"Subject:{subject}\n\n{message}\n"
    )

任何解决字符“\’”问题的建议。 谢谢。

排序

我使用以下代码:

进口重新

tweeter = re.sub(r'[^\\x00-\\x7f]',r' ',tweeter)

并修复了脚本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM