2015-06-24 73 views
0

有沒有辦法檢查下面的代碼中的HTTP狀態碼,因爲我沒有使用requesturllib庫,這將允許這樣做。檢查HTTP狀態(Python)

from pandas.io.excel import read_excel 

url = 'http://www.bankofengland.co.uk/statistics/Documents/yieldcurve/uknom05_mdaily.xls' 

#check the sheet number, spot: 9/9, short end 7/9 
spot_curve = read_excel(url, sheetname=8) #Creates the dataframes 
short_end_spot_curve = read_excel(url, sheetname=6) 

# do some cleaning, keep NaN for now, as forward fill NaN is not recommended for yield curve 
spot_curve.columns = spot_curve.loc['years:'] 
valid_index = spot_curve.index[4:] 
spot_curve = spot_curve.loc[valid_index] 
# remove all maturities within 5 years as those are duplicated in short-end file 
col_mask = spot_curve.columns.values > 5 
spot_curve = spot_curve.iloc[:, col_mask] 
#Providing correct names 
short_end_spot_curve.columns = short_end_spot_curve.loc['years:'] 
valid_index = short_end_spot_curve.index[4:] 
short_end_spot_curve = short_end_spot_curve.loc[valid_index] 

# merge these two, time index are identical 
# ============================================== 
combined_data = pd.concat([short_end_spot_curve, spot_curve], axis=1, join='outer') 
# sort the maturity from short end to long end 
combined_data.sort_index(axis=1, inplace=True) 

def filter_func(group): 
    return group.isnull().sum(axis=1) <= 50 

combined_data = combined_data.groupby(level=0).filter(filter_func) 
+0

什麼是'read_excel'? – LittleQ

+0

@LittleQ編輯代碼。 – Jojo

+2

Pandas使用urllib獲取excel文件,並且似乎無法存儲HTTP響應,據我所見。難道你不能只使用請求來自己下載excel文件,然後將它傳遞到第一行?相關行:https://github.com/pydata/pandas/blob/2fea54af7699bfeda267d598010a4004b079cd49/pandas/io/excel.py#L185 – jonnybazookatone

回答

0

pandasread_excel嘗試使用urllib2.urlopenurllib.request.urlopen相反在py3x)打開URL,並得到響應的.read()立即無店鋪HTTP請求,如:

data = urlopen(url).read() 

雖然你只需要部分excel,pandas將每次下載整個excel 。所以,我投了@jonnybazookatone。

最好將excel存儲到本地,然後可以先檢查文件的狀態碼和md5以驗證數據完整性或其他。

+0

謝謝。最終我最終使用了「請求」。 – Jojo