Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中捕获错误的zip文件错误?_Python_Python 3.x - Fatal编程技术网

如何在python中捕获错误的zip文件错误?

如何在python中捕获错误的zip文件错误?,python,python-3.x,Python,Python 3.x,我正试图提取上个月的股票数据,当我的代码试图在没有交易的日子提取股票数据时,遇到了这个错误 代码在datetime.date(2020,7,26)停止执行,这是一个周日,因此没有股票交易 我的代码: 导入日期时间 起始日期时间导入日期 作为pd进口熊猫 从nsepy.history导入获取价格列表 今天=日期。今天() 昨天=今天-datetime.timedelta(天=1) bhav_copy=获取价格表(dt=昨天) 对于范围(1,30)内的i: date=昨天-datetime.time

我正试图提取上个月的股票数据,当我的代码试图在没有交易的日子提取股票数据时,遇到了这个错误

代码在
datetime.date(2020,7,26)
停止执行,这是一个周日,因此没有股票交易

我的代码:

导入日期时间
起始日期时间导入日期
作为pd进口熊猫
从nsepy.history导入获取价格列表
今天=日期。今天()
昨天=今天-datetime.timedelta(天=1)
bhav_copy=获取价格表(dt=昨天)
对于范围(1,30)内的i:
date=昨天-datetime.timedelta(天=i)
尝试:
价格=获取价格列表(dt=日期)
除BadZipFile作为e外:
持续
bhav_copy=bhav_copy.append(价格)
错误:

---------------------------------------------------------------------------
BadZipFile                                Traceback (most recent call last)
<ipython-input-108-37d75cc40921> in <module>
     13     try:
---> 14         prices = get_price_list(dt=date_)
     15     except BadZipFile as e:

~/anaconda3/lib/python3.7/site-packages/nsepy/history.py in get_price_list(dt, series)
    323     res = price_list_url(yyyy, MMM, dt.strftime("%d%b%Y").upper())
--> 324     txt = unzip_str(res.content)
    325     fp = six.StringIO(txt)

~/anaconda3/lib/python3.7/site-packages/nsepy/commons.py in unzip_str(zipped_str, file_name)
    121 
--> 122     zf = zipfile.ZipFile(file=fp)
    123     if not file_name:

~/anaconda3/lib/python3.7/zipfile.py in __init__(self, file, mode, compression, allowZip64, compresslevel)
   1257             if mode == 'r':
-> 1258                 self._RealGetContents()
   1259             elif mode in ('w', 'x'):

~/anaconda3/lib/python3.7/zipfile.py in _RealGetContents(self)
   1324         if not endrec:
-> 1325             raise BadZipFile("File is not a zip file")
   1326         if self.debug > 1:

BadZipFile: File is not a zip file

During handling of the above exception, another exception occurred:

NameError                                 Traceback (most recent call last)
<ipython-input-108-37d75cc40921> in <module>
     13     try:
     14         prices = get_price_list(dt=date_)
---> 15     except BadZipFile as e:
     16         continue
     17     bhav_copy = bhav_copy.append(prices)

NameError: name 'BadZipFile' is not defined

---------------------------------------------------------------------------
BadZipFile回溯(最近一次调用上次)
在里面
13尝试:
--->14价格=获取价格列表(dt=日期)
15除BadZipFile作为e外:
获取价格列表中的~/anaconda3/lib/python3.7/site-packages/nsepy/history.py(dt系列)
323 res=price_list_url(yyyy,MMM,dt.strftime(“%d%b%Y”).upper())
-->324 txt=解压字符串(res.content)
325 fp=6.StringIO(txt)
解压缩str中的~/anaconda3/lib/python3.7/site-packages/nsepy/commons.py(压缩str,文件名)
121
-->122 zf=zipfile.zipfile(文件=fp)
123如果不是文件名:
~/anaconda3/lib/python3.7/zipfile.py在uuu init_uuu中(self,file,mode,compression,allowZip64,compresslevel)
1257如果模式='r':
->1258 self._RealGetContents()
1259('w','x')中的elif模式:
RealGetContents(self)中的~/anaconda3/lib/python3.7/zipfile.py
1324如果不是endrec:
->1325 raise BadZipFile(“文件不是zip文件”)
1326如果self.debug>1:
BadZipFile:文件不是zip文件
在处理上述异常期间,发生了另一个异常:
NameError回溯(最近一次呼叫上次)
在里面
13尝试:
14价格=获取价格列表(dt=日期)
--->15除BadZipFile作为e外:
16继续
17 bhav_copy=bhav_copy.append(价格)
NameError:未定义名称“BadZipFile”
我会试着用一个试着抓住的方块。我导入了
BadZipFile
模块,但在尝试捕获异常时出错


下面是for循环后的一段代码,我想我不知道如何在这里使用try和catch,希望您能提供帮助。

您需要导入您试图捕获的错误:

从zipfile导入BadZipFile

您需要导入要捕获的错误:

从zipfile导入BadZipFile

我也使用了该库,但无法捕获@SamMorgan的异常。我也更新了我的问题。刚刚用我的建议测试了你的代码,它完全按照预期执行。将这一行添加到代码顶部并尝试一下。我也使用了该库,但无法捕获异常@SamMorgan。我也更新了我的问题。刚刚用我的建议测试了你的代码,它完全按照预期执行。将该行添加到代码顶部并尝试一下。请包括对我的问题进行否决的原因,我会适当地更新或更改我的问题。您的代码,现在包括来自zipfile import BadZipFile的
,将不会生成错误
名称错误:未定义名称“BadZipFile”
。由于您已编辑了问题的某些部分,而保留了其他部分,因此没有意义。请说明对我的问题进行否决的原因,我将适当地更新或更改我的问题。您的代码,现在包括来自zipfile import BadZipFile的
,不会生成错误
name错误:未定义名称“BadZipFile”
。由于您编辑了问题的某些部分,而保留了其他部分,因此没有意义。