Python 3.8--什么是'with'语句?

问题描述 投票:-1回答:1
for i in range(0,len(periods)):

        coeffs = []

        for j in range(periods[i],len(prices)-periods[i]):

            x = np.arange(0,periods[i])
            y = detrended.iloc[j-periods[i]:j



  with warnings.catch_warnings():
            warnings.simplefilter('error',OptimizeWarning)

            try:
                res = scipy.optimize.curve_fit(fseries,x,y)

            except (RuntimeError, OptimizeWarning):
                res = np.empty(1,4)
                res[0,:] = np.NAN

            if plot == True:
                xt = np.linspace(0,periods[i],100)
                yt = fseries(xt,res[0][0],res[0][1],res[0][2],res[0][3])

                plt.plot(x,y)
                plt.plot(xt,yt,'r')

                plt.show()

            coeffs = np.append(coeffs,res[0],axis=0)
        warnings.filterwarnings('ignore',category=np.VisibleDeprecationWarning)
python try-catch
1个回答
0
投票

不需要为'with'语句添加'from'语句。未来 在程序的第一行导入with_statement',然后就可以了。

© www.soinside.com 2019 - 2024. All rights reserved.