- 安装库
pip install pandas
# 读取.xls
pip install xlrd
# 读取.xlsx
pip install openpyxl
- 案例1
import pandas as pd
path = r"C:\work\test.xlsx"
data = pd.read_excel(path)
print(data)
- 读取.csv文件
import pandas as pd
data = pd.read_csv(r"C:\\work\\test.csv")
print(data)
print(type(data))
- 注意excel文件另存为csv后缀的文件后,需要再用记事本打开,查看编码格式是否为utf8,否则会报错
- 解决方案:在使用记事本另存为utf8的格式
Traceback (most recent call last):
File "C:\work\PythonProject\demo\day15\pandas学习01.py", line 27, in <module>
data = pd.read_csv(r"C:\\work\\test.csv")
File "C:\Users\ychen\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 948, in read_csv
return _read(filepath_or_buffer, kwds)
File "C:\Users\ychen\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 611, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "C:\Users\ychen\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 1448, in __init__
self._engine = self._make_engine(f, self.engine)
File "C:\Users\ychen\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 1723, in _make_engine
return mapping[engine](f, **self.options)
File "C:\Users\ychen\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\c_parser_wrapper.py", line 93, in __init__
self._reader = parsers.TextReader(src, **kwds)
File "parsers.pyx", line 579, in pandas._libs.parsers.TextReader.__cinit__
File "parsers.pyx", line 668, in pandas._libs.parsers.TextReader._get_header
File "parsers.pyx", line 879, in pandas._libs.parsers.TextReader._tokenize_rows
File "parsers.pyx", line 890, in pandas._libs.parsers.TextReader._check_tokenize_status
File "parsers.pyx", line 2050, in pandas._libs.parsers.raise_parser_error
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb2 in position 0: invalid start byte
标签:入门,Pandas,parsers,File,._,line,csv,pandas
From: https://www.cnblogs.com/dogleftover/p/17823856.html