为什么 pandas.read_excel 不会运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/21947813/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why won't pandas.read_excel run?
提问by cubedNnoobed
I am trying to use pandas.read_excel but I keep getting " 'module' object has no attribute 'read_excel' " as an error in my terminal as shown
我正在尝试使用 pandas.read_excel 但我不断收到“'module' object has no attribute 'read_excel'”作为我终端中的错误,如图所示
  File "read.py", line 9, in <module>
  cols = pd.read_excel('laucnty12', 'Poverty Data', index_col='State', \    na_values=['NA'])
  AttributeError: 'module' object has no attribute 'read_excel'
I have tried pd.read_excel() and pd.io.parsers.read_excel() but get the same error. I have python 2.7 installed and other parts of pandas work fine such as xls.parse and read_csv. My code is below:
我试过 pd.read_excel() 和 pd.io.parsers.read_excel() 但得到同样的错误。我安装了 python 2.7,pandas 的其他部分工作正常,例如 xls.parse 和 read_csv。我的代码如下:
import pandas as pd
from pandas import *
xls = pd.ExcelFile('laucnty12.xls')
data = xls.parse('laucnty12', index_col=None, na_values=['NA'])
cols = pd.read_excel('laucnty12', 'Poverty Data', index_col='State', na_values=['NA'])
print cols
回答by asamarin
You probably mean pd.io.excel.read_excel()
你可能是说 pd.io.excel.read_excel()
回答by charlesreid1
The problem is that your script is called "read.py". The Python file that defines read_excel already imports another module called "read" - so when you try and run your "read.py" script, it squashes the old "read" module that pandas is using, and thus breaks read_excel. This problem can happen with other "common" short names for scripts, like "email.py".
问题是你的脚本被称为“read.py”。定义 read_excel 的 Python 文件已经导入了另一个名为“read”的模块 - 因此,当您尝试运行“read.py”脚本时,它会压缩Pandas正在使用的旧“read”模块,从而破坏 read_excel。此问题可能发生在脚本的其他“常见”短名称上,例如“email.py”。
Try renaming your script.
尝试重命名您的脚本。
回答by Anukals
df = pd.read_excel(filepath + 'Result.xlsx')
Check whether the extension of excel file is xlsor xlsxthen add the same in the query. I tried and its is working fine now.
检查是否Excel文件的扩展名xls或xlsx再加入相同的查询。我试过了,现在工作正常。

