python - 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:无效的继续字节
解决方案
推荐阅读
- json - Jira Build Status Gadget 给出 JSON 解析错误
- php - 使用ajax和php将数据插入postgresql数据库
- aws-sam - 向 Serverless.Template 文件添加参数
- python - 来自 groupby 和 count 的堆叠条形图
- javascript - 使用 Jest 进行 React 组件测试
- android - 设计在 Android Studio Preview 中完美展示,但在真实设备中却没有
- automated-tests - 如何访问在 SpecFlow 中的 AfterScenario 钩子步骤中创建的类的实例?
- android - 为什么 androidx.navigation 使用片段的替换?
- node.js - Angular CLI:npm 错误!超出最大调用堆栈大小
- python - Numpy将子数组的索引返回到数组中