python - 无法从 Python 3.6 读取 excel 文件
问题描述
我无法读取 excel 文件。我有 MS Excel 2016。
import pandas as pd
with open(r'dummy.xlsx','r') as exl:
p = pd.ExcelFile(exl)
print(p.sheet_names)
runfile('C:/Users/ESETPRI/Documents/My Python Scripts/pdf read.py', wdir='C:/Users/ESETPRI/Documents/My Python Scripts')
Traceback (most recent call last):
File "<ipython-input-13-eb59316a92ec>", line 1, in <module>
runfile('C:/Users/ESETPRI/Documents/My Python Scripts/pdf read.py', wdir='C:/Users/ESETPRI/Documents/My Python Scripts')
File "C:\Users\ESETPRI\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\Users\ESETPRI\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/ESETPRI/Documents/My Python Scripts/pdf read.py", line 11, in <module>
p = pd.ExcelFile(exl)
File "C:\Users\ESETPRI\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\excel.py", line 291, in __init__
data = io.read()
File "C:\Users\ESETPRI\AppData\Local\Continuum\anaconda3\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 634: character maps to <undefined>
尝试了不同类型的编码 UTF-8、UTF-16、ASCII、CP1252 但没有成功。即使尝试使用空的 xlsx 文件,结果也相同。
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa4 in position 14: invalid start byte
UnicodeDecodeError: 'utf-16-le' codec can't decode bytes in position 602-603: illegal encoding
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 634: character maps to <undefined>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xa4 in position 14: ordinal not in range(128)
编辑
也试过'latin-1'
File "C:\Users\ESETPRI\AppData\Local\Continuum\anaconda3\lib\site-packages\xlrd\book.py", line 672, in get2bytes
return (BYTES_ORD(hi) << 8) | BYTES_ORD(lo)
TypeError: unsupported operand type(s) for <<: 'str' and 'int'
解决方案
从文档尝试read_excel
>>> pd.read_excel('tmp.xlsx')
Name Value
0 string1 1
1 string2 2
2 string3 3
推荐阅读
- r - 在 r 中,如何根据列表元素对数据框的某些列降序和其他列升序进行排名?
- excel - Microsoft Forms 2.0 对象库
- reactjs - ESLint SyntaxError:无效的正则表达式标志,Regex
- react-native - 我应该将 Expo 用于 expo 音频库还是 React-native-track-player?
- silverstripe - 用于 ModelAdmin 类的 SilverStripe 自定义 csv/excel 导出功能
- asp.net - ASP.NET MVC CORE Web 应用程序集成测试与 EF Core 内存数据库 - 每次测试的新数据库
- django - TypeError:预期的字符串或类似字节的对象,尽管未使用日期时间字段 Django
- java - 如何在自己的线程中启动 JavaFX GUI
- python - ValueError:检查输入时出错:预期 input_3 的形状为 (34,),但得到的数组的形状为 (36,)
- api - Fleet Api 与 Route API 不同 - POI 无效