python 导入数据错误:UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xb5 in position 0: invalid start
python,导入,数据,错误,UnicodeDecodeError,utf,codec,can,decode,byte,0xb5,in,position,invalid,start
2025-04-11 08:58:02 时间
大家好,又见面了,我是你们的朋友全栈君。
找到这个教程
https://www.cnblogs.com/OliverQin/p/8966321.html
我要导入CSV文件,已经放在相同目录之下。
import pandas as pd
data = pd.read_csv("电信客户流失.csv",encoding="utf8")
报错如下
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-5-6f8087e333bd> in <module>()
1 import pandas as pd
----> 2 data = pd.read_csv("电信客户流失.csv",encoding="utf-8")
/usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, escapechar, comment, encoding, dialect, tupleize_cols, error_bad_lines, warn_bad_lines, skipfooter, doublequote, delim_whitespace, low_memory, memory_map, float_precision)
676 skip_blank_lines=skip_blank_lines)
677
--> 678 return _read(filepath_or_buffer, kwds)
679
680 parser_f.__name__ = name
/usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py in _read(filepath_or_buffer, kwds)
438
439 # Create the parser.
--> 440 parser = TextFileReader(filepath_or_buffer, **kwds)
441
442 if chunksize or iterator:
/usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py in __init__(self, f, engine, **kwds)
785 self.options['has_index_names'] = kwds['has_index_names']
786
--> 787 self._make_engine(self.engine)
788
789 def close(self):
/usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py in _make_engine(self, engine)
1012 def _make_engine(self, engine='c'):
1013 if engine == 'c':
-> 1014 self._engine = CParserWrapper(self.f, **self.options)
1015 else:
1016 if engine == 'python':
/usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py in __init__(self, src, **kwds)
1706 kwds['usecols'] = self.usecols
1707
-> 1708 self._reader = parsers.TextReader(src, **kwds)
1709
1710 passed_names = self.names is None
pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.__cinit__()
pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._get_header()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb5 in position 0: invalid start byte
错误类型
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb5 in position 0: invalid start byte
过了一会儿突然可以了,真实奇怪
这时我在看这篇博客,
https://blog.csdn.net/xxceciline/article/details/80405129
然后我再次运行,又出现错误
使用这个链接的方法
https://www.cnblogs.com/pengei/p/6407077.html
==========解决方案============
第一种:
第一行加:# -*- coding: utf-8 -*-
第二种:
引用处设置默认编码格式
# coding: utf-8
第三种:
使用中文处将中文unicode编码
python3以后第二种方式。
还是一样的报错
#-*- coding : utf-8 -*-
# coding: utf-8
import pandas as pd
data = pd.read_csv("电信客户流失.csv",encoding="utf-8")
我尝试这个方法行不通
在百度上的方法都解决不了,我用谷歌搜索解决方案
我的最终解决方案,来自这里
#-*- coding : utf-8 -*-
# coding: utf-8
import pandas as pd
data = pd.read_csv("电信客户流失.csv",encoding="unicode_escape")
说明刚才的错误没有了
用read_csv之后,里面加上encoding=’gbk’就可以了。
现在是什么问题都没有了
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/160459.html原文链接:https://javaforall.cn
相关文章
- python Input输入
- Python-drf前戏38.4-前端Vue04
- 破14亿,Python分析我国存在哪些人口危机!「建议收藏」
- Python 让我舅舅的书法作品和 PIL 库发生点美的误会
- Python 从底层结构聊 Beautiful Soup 4(内置豆瓣最新电影排行榜爬取案例)!
- python anaconda jupyter_anaconda和pip
- Python 细聊!可以媲美 PS 的 PIL 图片处理库
- Python 基于 selenium 实现不同商城的商品价格差异分析系统
- 用Python做图像处理[通俗易懂]
- PyAOS:大气和海洋科学Python社区
- Python 实现大整数乘法算法
- pythoncharm注释快捷键_多行注释以什么开头
- 如何理解python报错信息_csb报错
- Python 语法基础
- Python数据结构
- Python 随机(Random)模块的不可预测之美
- python开发中重写父类的方法有哪些?
- Python-辨析type/dtype/astype用法
- Python 图_系列之基于<链接表>实现无向图最短路径搜索
- Python概述