15

.xls ファイルを指す URL リンクを読み取るさまざまな方法を検討した後、xlrd を使用することにしました。

「xlrd.book.Book」タイプを「pandas.DataFrame」に変換するのに苦労しています

私は次のものを持っています:

import pandas
import xlrd 
import urllib2

link ='http://www.econ.yale.edu/~shiller/data/chapt26.xls'
socket = urllib2.urlopen(link)

#this line gets me the excel workbook 
xlfile = xlrd.open_workbook(file_contents = socket.read())

#storing the sheets
sheets = xlfile.sheets()

sheetsの最後のシートを取り、としてインポートしたいのですがpandas.DataFrame、これを達成する方法について何かアイデアはありますか? 試してみましpandas.ExcelFile.parse()たが、Excel ファイルへのパスが必要です。確かにファイルをメモリに保存してから解析することはできますが(tempfileまたは何かを使用して)、Pythonのガイドラインに従って、すでにパンダに書き込まれている可能性が高い機能を使用しようとしています。

いつものように、どんなガイダンスも大歓迎です。

4

2 に答える 2

26

socketに渡すことができますExcelFile

>>> import pandas as pd
>>> import urllib2
>>> link = 'http://www.econ.yale.edu/~shiller/data/chapt26.xls'
>>> socket = urllib2.urlopen(link)
>>> xd = pd.ExcelFile(socket)
NOTE *** Ignoring non-worksheet data named u'PDVPlot' (type 0x02 = Chart)
NOTE *** Ignoring non-worksheet data named u'ConsumptionPlot' (type 0x02 = Chart)
>>> xd.sheet_names
[u'Data', u'Consumption', u'Calculations']
>>> df = xd.parse(xd.sheet_names[-1], header=None)
>>> df
                                   0   1   2   3         4
0        Average Real Interest Rate: NaN NaN NaN  1.028826
1    Geometric Average Stock Return: NaN NaN NaN  0.065533
2              exp(geo. Avg. return) NaN NaN NaN  0.067728
3  Geometric Average Dividend Growth NaN NaN NaN  0.012025
于 2013-03-23T16:00:15.390 に答える