本人python小白,有一個事情是要分析一下excel表格中的數據,excel中數據量巨大,直接用眼睛看不太現實,因此準備寫個python腳本來操作,本來以爲這個事情很簡單,只要寫好python代碼然後運行即可。沒想到踩了不少的坑。現記錄如下:
python源碼如下:#readexcel.py
#!/usr/bin/env python3
# coding=UTF-8
import xlrd
from datetime import date,datetime
file = 'test.xlsx'
def read_excel():
wb = xlrd.open_workbook(filename=file)#打開文件
print(wb.sheet_names())#獲取所有表格名字
sheet1 = wb.sheet_by_index(0)#通過索引獲取表格
sheet2 = wb.sheet_by_name('Sheet2')#通過名字獲取表格
print(sheet1,sheet2)
print(sheet1.name,sheet1.nrows,sheet1.ncols)
rows = sheet1.row_values(2)#獲取行內容
cols = sheet1.col_values(3)#獲取列內容
print(rows)
print(cols)
print(sheet1.cell(1,0).value)#獲取表格裏的內容,三種方式
print(sheet1.cell_value(1,0))
print(sheet1.row(1)[0].value)
if __name__ == '__main__':
read_excel()
Excel的數據如下所示(一些測試數據):
Mac終端運行:python readexcel.py
提示如下錯誤:
Traceback (most recent call last):
File "readexcel.py", line 4, in <module>
import xlrd
ImportError: No module named xlrd
ok,操作excel的工具xlrd,不屬於python標準庫,需要單獨安裝,命令爲:
sudo pip3 install xlrd
提示如下錯誤:
sudo: pip: command not found
好,繼續安裝pip
sudo easy_install pip
看到如下信息:
Searching for pip
Reading https://pypi.python.org/simple/pip/
Download error on https://pypi.python.org/simple/pip/: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:590) -- Some packages may not be found!
Couldn't find index page for 'pip' (maybe misspelled?)
Scanning index of all packages (this may take a while)
Reading https://pypi.python.org/simple/
Download error on https://pypi.python.org/simple/: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:590) -- Some packages may not be found!
No local packages or download links found for pip
error: Could not find suitable distribution for Requirement.parse('pip')
沒有找到pip,爲什麼呢,打開https://pypi.python.org/simple/pip/ 該網址發現,目標網址已經被重定向了,查看easy_install的源碼,發現難以直接找到該網址的設置位置。換別的方法:
sudo curl https://bootstrap.pypa.io/get-pip.py | python
收到如下信息:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1662k 100 1662k 0 0 99k 0 0:00:16 0:00:16 --:--:-- 80053
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Collecting pip
Using cached https://files.pythonhosted.org/packages/d7/41/34dd96bd33958e52cb4da2f1bf0818e396514fd4f4725a79199564cd0c20/pip-19.0.2-py2.py3-none-any.whl
Collecting wheel
Using cached https://files.pythonhosted.org/packages/7c/d7/20bd3c501f53fdb0b7387e75c03bd1fce748a1c3dd342fc53744e28e3de1/wheel-0.33.0-py2.py3-none-any.whl
Installing collected packages: pip, wheel
Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/pip'
Consider using the `--user` option or check the permissions.
權限不足,好吧,仔細思考下,應該是python的權限不足,改爲如下指令:
sudo curl https://bootstrap.pypa.io/get-pip.py | sudo python
如下所示,安裝成功
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1662k 100 1662k 0 0 75235 0 0:00:22 0:00:22 --:--:-- 72068
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
The directory '/Users/wangguoqiang/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/wangguoqiang/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting pip
Downloading https://files.pythonhosted.org/packages/d7/41/34dd96bd33958e52cb4da2f1bf0818e396514fd4f4725a79199564cd0c20/pip-19.0.2-py2.py3-none-any.whl (1.4MB)
100% |████████████████████████████████| 1.4MB 221kB/s
Collecting wheel
Downloading https://files.pythonhosted.org/packages/7c/d7/20bd3c501f53fdb0b7387e75c03bd1fce748a1c3dd342fc53744e28e3de1/wheel-0.33.0-py2.py3-none-any.whl
Installing collected packages: pip, wheel
Successfully installed pip-19.0.2 wheel-0.33.0
然後繼續:
sudo pip install xlrd
也提示安裝成功:
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
The directory '/Users/wangguoqiang/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/wangguoqiang/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting xlrd
Downloading https://files.pythonhosted.org/packages/b0/16/63576a1a001752e34bf8ea62e367997530dc553b689356b9879339cf45a4/xlrd-1.2.0-py2.py3-none-any.whl (103kB)
100% |████████████████████████████████| 112kB 335kB/s
Installing collected packages: xlrd
Successfully installed xlrd-1.2.0
OK,終於看到了曙光,運行一下試試:
python readexcel.py
成了:
[u'Sheet1', u'Sheet2']
(<xlrd.sheet.Sheet object at 0x1021fc350>, <xlrd.sheet.Sheet object at 0x1021fc390>)
(u'Sheet1', 4, 4)
[123.0, u'def', u'123def', u'def123']
[u't4', u'abc123', u'def123', u'def1232']
123.0
123.0
123.0