首页 > 解决方案 > Python UnicodeDecodeError:'utf-8'编解码器无法解码位置 670 中的字节 0xc4:无效的继续字节

问题描述

我查看了 Stackoverflow,但不幸的是,我查找的每个主题都没有提供此问题的解决方案。所以我想当我在 pd.read_csv 中添加“encoding = 'utf-8'”或拉丁语等时,它应该可以解决问题,但它不会。那么为什么会出现这个错误呢?

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import pandas as pd
import plotly.graph_objs as go
import numpy as np

df = pd.read_csv("https://opendata.ecdc.europa.eu/covid19/casedistribution/csv", index_col = 0, parse_dates = True, encoding="ISO-8859-1")


df["cases"] = pd.to_numeric(df["cases"])
df["deaths"] = pd.to_numeric(df["deaths"])
df["deaths by cases"] = df["deaths"].divide(df["cases"])
df = df.replace(np.inf, 0)
df = df.replace(np.nan,0)

df.rename(columns={"Cumulative_number_for_14_days_of_COVID-19_cases_per_100000" : "cum per 100000"}, 
           inplace=True)


d = {1:"Januar", 2:"Februar", 3:"März", 4:"April", 5:"Mai", 6:"Juni", 7:"Juli"}
df.month = df.month.map(d)

df= df.pivot_table(values = "deaths by cases", index = ["month", "countriesAndTerritories"])
df = df.reset_index()

print(df)
data = [go.Scatter(
    x = df["month"],
    y = df["deaths by cases"],
    mode = 'markers',
        )]
layout = go.Layout(
    title = 'Random Data Scatterplot', 
    xaxis = dict(title = 'Some random x-values'), 
    yaxis = dict(title = 'Some random y-values'), 
    hovermode ='closest' 
    )
fig = go.Figure(data=data, layout=layout)
pyo.plot(fig, filename='scatter3.html')

代码的错误回溯:

runfile('C:/Users/Asus/Desktop/Business Intelligence Learning/Python Dash/Plotly-Dashboards-mit-Dash-v2/Plotly-Dashboards-mit-Dash-v2/1-02-Streudiagramm/scatter3.py', wdir='C:/Users/Asus/Desktop/Business Intelligence Learning/Python Dash/Plotly-Dashboards-mit-Dash-v2/Plotly-Dashboards-mit-Dash-v2/1-02-Streudiagramm') Traceback (最新最后打电话):

文件“C:\Users\Asus\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py”,第 3343 行,在 run_code self.showtraceback(running_compiled_code=True)

文件“C:\Users\Asus\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py”,第 2026 行,在 showtraceback self.showsyntaxerror(filename, running_compiled_code)

文件“C:\Users\Asus\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py”,第 2088 行,showsyntaxerror stb = self.SyntaxTB.structured_traceback(etype, value, elist)

文件“C:\Users\Asus\Anaconda3\lib\site-packages\IPython\core\ultratb.py”,第 1420 行,在structured_traceback newtext = linecache.getline(value.filename, value.lineno)

文件“C:\Users\Asus\Anaconda3\lib\linecache.py”,第 16 行,在 getline 行 = getlines(filename, module_globals)

文件“C:\Users\Asus\Anaconda3\lib\linecache.py”,第 47 行,在 getlines 中返回 updatecache(filename, module_globals)

文件“C:\Users\Asus\Anaconda3\lib\linecache.py”,第 137 行,在 updatecache 行 = fp.readlines()

文件“C:\Users\Asus\Anaconda3\lib\codecs.py”,第 322 行,在 decode (result, used) = self._buffer_decode(data, self.errors, final)

UnicodeDecodeError:“utf-8”编解码器无法解码位置 666 中的字节 0xc4:无效的继续字节

标签: pythonpython-3.xcharacter-encodingipythonpython-unicode

解决方案


推荐阅读