如何解决UnicodeEncodeError:'charmap'编解码器无法对位置131-132中的字符进行编码:字符映射为<undefined>
你好!放心,我正在学习Python,这是我的第一篇文章。这里发生了什么?!该网站是使用从Google表格中提取的python脚本构建的。我突然开始收到此错误:
Downloading: Sheet1
Traceback (most recent call last):
File "C:\Users\Elyse\Desktop\my_pokemon_stats\site_builder.py",line 64,in <module>
download_data()
File "C:\Users\Elyse\Desktop\my_pokemon_stats\site_builder.py",line 31,in download_data
writer.writerows(sheet_values)
File "c:\python39\lib\encodings\cp1252.py",line 19,in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 131-132: character maps to <undefined>
这是site_builder.py代码:
import csv
import boto3
import gspread
import jinja2
from oauth2client.service_account import ServiceAccountCredentials
AWS_PROFILE = "default"
BUCKET = "newsin.it"
WORKBOOK = "NewsinIT"
def download_data():
"""Download data using the Google Sheets API"""
scope = [
"https://spreadsheets.google.com/Feeds","https://www.googleapis.com/auth/drive",]
credentials = ServiceAccountCredentials.from_json_keyfile_name(
"credentials.json",scope
)
client = gspread.authorize(credentials)
worksheet = client.open(WORKBOOK).get_worksheet(0)
sheet_values = worksheet.get_all_values()
print(f"Downloading: {worksheet.title}")
with open("my_pokemon_stats.csv","w") as f:
writer = csv.writer(f)
writer.writerows(sheet_values)
def generate_site():
"""Generate site in local directory"""
print("Process data and build site")
template_loader = jinja2.FileSystemloader(searchpath="./")
template_env = jinja2.Environment(loader=template_loader)
template = template_env.get_template("template.html")
with open("my_pokemon_stats.csv") as csv_file:
csv_reader = csv.DictReader(csv_file)
data = [row for row in csv_reader]
output = template.render(data=data)
with open("index.html","w") as f:
f.write(output)
def deploy_site():
"""Deploy site S3 bucket"""
print("Upload data to S3")
session = boto3.Session(profile_name=AWS_PROFILE)
s3 = session.resource("s3")
s3.Bucket(BUCKET).upload_file(
Filename="index.html",Key="index.html",ExtraArgs={"ContentType": "text/html"}
)
if __name__ == "__main__":
download_data()
generate_site()
deploy_site()
解决方法
尝试此操作,我已更新代码并在注释中添加了我作为 #Added编码
.product {
&__header {
font-size: 1.7rem
// etc...
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。