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

时间:2020-11-04 14:02:30

标签: python runtime-error

你好!放心,我正在学习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()

正在发生什么,我需要添加什么来解决此问题?

1 个答案:

答案 0 :(得分:0)

尝试此操作,我已更新代码并在注释中添加了我作为 #Added编码

.product {
    &__header {
      font-size: 1.7rem
      // etc...
    }
 }