有的小伙伴使用Python时发现自己没有xlrd,不要着急,我们一起来解决。
我目前正在使用PyCharm与Python版本3.4.3进行此特定项目。
这个PyCharm以前有Python2.7,我升级到3.4.3.
我正在尝试使用Pandas从Excel文件中获取数据。
代码:
1 2 3 4 | <p style= "line-height: 1.75em;" ><span style= "font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;" >import pandas as pd
df = pd.read_excel( "File.xls" , "Sheet1" )
print (df)<br></span></p>
|
当我运行此代码时,我收到此错误
1 | <p style= "line-height: 1.75em;" ><span style= "font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;" >ImportError: No module named 'xlrd' <br></span></p>
|
我搜索了Stackoverflow并找到了一些建议:我试过了
1 | <p style= "line-height: 1.75em;" ><span style= "font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;" >pip install xlrd<br></span></p>
|
但是,当我这样做时,消息说
1 | <p style= "line-height: 1.75em;" ><span style= "font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;" > "Requirement already satisfied: xlrd in ./anaconda2/usr/lib/python2.7/site-packages" <br></span></p>
|
解决:
去终端(使用Linux),然后输入
1 | <p style= "line-height: 1.75em;" ><span style= "font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;" >sudo pip3 install xlrd<br></span></p>
|
然后我在python中导入xlrd并使用相同的代码:
1 2 | <p style= "line-height: 1.75em;" ><span style= "font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;" >df = pd.read_excel( "File.xlsx" , "Sheet1" )
print (df)<br></span></p>
|
学会了的小伙伴可以保存起来,方便下次使用。更多Python学习推荐:PyThon学习网教学中心。