【发布时间】:2015-09-12 10:05:18
【问题描述】:
我试图了解如何在下面的代码中处理http.client.IncompleteRead Error。我使用this post 中的想法处理错误。基本上,我认为这可能只是服务器限制了我可以访问数据的次数,但奇怪的是,我有时会收到 200 的 HTTP 状态代码,但下面的代码仍然返回 None 类型。这是因为出现错误时zipdata = e.partial 没有返回任何内容吗?
def update(self, ABBRV):
if self.ABBRV == 'cd':
try:
my_url = 'http://www.bankofcanada.ca/stats/results/csv'
data = urllib.parse.urlencode({"lookupPage": "lookup_yield_curve.php",
"startRange": "1986-01-01",
"searchRange": "all"})
binary_data = data.encode('utf-8')
req = urllib.request.Request(my_url, binary_data)
result = urllib.request.urlopen(req)
print('status:: {},{}'.format(result.status, my_url))
zipdata = result.read()
zipfile = ZipFile(BytesIO(zipdata))
df = pd.read_csv(zipfile.open(zipfile.namelist()[0]))
df = pd.melt(df, id_vars=['Date'])
return df
#In case of http.client.IncompleteRead Error
except http.client.IncompleteRead as e:
zipdata = e.partial
谢谢
【问题讨论】:
-
你能添加你的import语句吗? (例如
from zipfile import ZipFile)当我不得不花 5 分钟来计算提问者正在使用的库以便我可以运行代码并帮助他们时,这是我的主要烦恼。
标签: python pandas exception-handling