UnicodeEncodeError:'charmap'编解码器无法对位置 131-132 中的字符进行编码:字符映射到<undefined>



你好!请放心,我正在学习Python,这是我的第一篇文章。这是怎么回事?!该网站是使用从Google Sheets中提取的python脚本构建的。我突然开始收到这个错误:

Downloading: Sheet1
Traceback (most recent call last):
File "C:UsersElyseDesktopmy_pokemon_statssite_builder.py", line 64, in <module>
download_data()
File "C:UsersElyseDesktopmy_pokemon_statssite_builder.py", line 31, in download_data
writer.writerows(sheet_values)
File "c:python39libencodingscp1252.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()

发生了什么,我需要添加什么来修复此问题

试试这个,我已经更新了代码,并添加了我编辑为#添加了编码的注释

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}")
#Added encoding 
with open("my_pokemon_stats.csv", "w",encoding="utf-8")) 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")
#Added encoding 
with open("my_pokemon_stats.csv",encoding="utf-8") as csv_file:
csv_reader = csv.DictReader(csv_file)
data = [row for row in csv_reader]
output = template.render(data=data)
#Added encoding 
with open("index.html", "w",encoding="utf-8") 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()

最新更新