2012/02/09
PythonでExcelファイルを読む
by
seko
—
posted at
2012-02-09 22:36
last modified
2012-02-09 22:36
今回は、 xlrd というモジュールを使ってみました。 Python-izm そのままです。
インタラクティブシェルの IPython を初めて使ってみました。
In [3]: import xlrd
In [4]: book = xlrd.open_workbook('fruitslst.xls')
In [5]: print book.nsheets
3
In [6]: for name in book.sheet_names():
...: print name
...:
Sheet1
Sheet2
Sheet3
In [7]: print book.sheet_by_index(0).name
Sheet1
In [9]: print book.sheet_by_name('Sheet2').name
Sheet2
In [10]: sheet_1 = book.sheet_by_index(0)
In [11]: for col in range(sheet_1.ncols):
....: print '-' * 20
....: for row in range(sheet_1.nrows):
....: print sheet_1.cell(row,col).value
....:
--------------------
Apple
Orenge
Melon
--------------------
3.0
2.0
1.0
- Category(s)
- python
- The URL to Trackback this entry is:
- http://lightson.dip.jp/blog/seko/2818/tbping