简体   繁体   English

无法从Python 3.6读取Excel文件

[英]Unable to read excel file from Python 3.6

I am unable to read excel files. 我无法读取Excel文件。 I am having MS Excel 2016. 我正在使用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>

Tried different type of encoding UTF-8, UTF-16, ASCII, CP1252 but no success. 尝试了不同类型的编码UTF-8,UTF-16,ASCII,CP1252,但没有成功。 Even the tried with an empty xlsx file, same result. 即使尝试使用空的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)

EDIT 编辑

Tried 'latin-1' as well 也尝试过“ 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'

From the docs try read_excel 文档尝试read_excel

>>> pd.read_excel('tmp.xlsx')
      Name  Value
0  string1      1
1  string2      2
2  string3      3

try this 尝试这个

With pandas 与熊猫

workbook_df=pd.ExcelFile(input_file_path)
key_sheet = workbook_df.parse("KEY")

With openpyxl 用openpyxl

wb = openpyxl.load_workbook(input_file_path)
write_output = wb.get_sheet_by_name('OUTPUT')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM