简体   繁体   English

为什么我无法在同一Python块中同时捕获NameError和UnboundLocalError?

[英]Why am I not able to catch both NameError and UnboundLocalError in the same except block in Python?

A little background-- I have a Python program that makes plots from CSV files. 有一点背景-我有一个Python程序,可以从CSV文件进行绘图。 I'm trying to make it more flexible by allowing a user to input between 1 and 3 files. 我试图通过允许用户输入1到3个文件来使其更加灵活。 I import the csv data into pandas data frames that are then used to generate a number of different plots. 我将csv数据导入到pandas数据框中,然后用于生成许多不同的图。

I'm sure there's a better way to do what I'm doing, but I came across something strange (to me, at least) in my initial attempt to figure out if a user had input less than the maximum number of files. 我敢肯定,有一种更好的方式来做我正在做的事情,但是在我最初尝试弄清楚用户输入的文件数是否少于最大数量时,遇到了一件奇怪的事情(至少对我来说)。

Here is how I import the data: 这是我导入数据的方式:

# Imports the data.  The first two rows must be skipped due to the file format
data1 = pd.read_csv(filename1, skiprows=1, header=True)
if filename2 != '':
    data2 = pd.read_csv(filename2, skiprows=1, header=True)
if filename3 != '':
    data3 = pd.read_csv(filename3, skiprows=1, header=True)

So data2 and data3 are only defined if a user has provided a file name from my GUI. 因此,仅当用户从我的GUI提供文件名时,才定义data2data3 Later, I just wanted to use the existence of data2 and data3 to determine whether or not to plot the 2nd and 3rd data sets, respectively: 后来,我只是想利用data2data3的存在来确定是否分别绘制第二和第三数据集:

    try:
        axarr[1, 0].psd(data1[Ynew], NFFT=n_samples, Fs=fs, noverlap=n_overlap, window=mlab.window_hanning, label=baseFileName1)
    except KeyError:
        axarr[1, 0].psd(data1[Yold], NFFT=n_samples, Fs=fs, noverlap=n_overlap, window=mlab.window_hanning, label=baseFileName1)
    try:
        axarr[1, 0].psd(data2[Ynew], NFFT=n_samples, Fs=fs, noverlap=n_overlap, window=mlab.window_hanning, label=baseFileName2)
    except (UnboundLocalError, NameError):
        pass
    except KeyError:
        axarr[1, 0].psd(data2[Yold], NFFT=n_samples, Fs=fs, noverlap=n_overlap, window=mlab.window_hanning, label=baseFileName2)
    try:
        axarr[1, 0].psd(data3[Ynew], NFFT=n_samples, Fs=fs, noverlap=n_overlap, window=mlab.window_hanning, label=baseFileName3)
    except (UnboundLocalError, NameError):
        pass
    except KeyError:
        axarr[1, 0].psd(data3[Yold], NFFT=n_samples, Fs=fs, noverlap=n_overlap, window=mlab.window_hanning, label=baseFileName3)

And here's where it got weird. 这就是奇怪的地方。 When I run the thing, it throws an UnboundLocalError telling me that 'NameError' is referenced before assignment. 当我运行该东西时,它会抛出UnboundLocalError,告诉我在分配之前已引用'NameError'。 So that except block that's supposed to catch the UnboundLocalError isn't passing like I would expect. 因此,除了应该捕获UnboundLocalError的块之外,没有像我期望的那样传递。 If I try to catch just the UnboundLocalError, a NameError is thrown. 如果我尝试仅捕获UnboundLocalError,则会引发NameError。 If I try to catch just a NameError, an UnboundLocalError is thrown. 如果我尝试仅捕获NameError,则会引发UnboundLocalError。 Can somebody explain to me what's going on here? 有人可以告诉我这是怎么回事吗?

EDIT- Here's the traceback: 编辑-这是回溯:

Error Traceback 错误回溯

I've revisited this multiple times and there seems to be bug report for this sort of problem floating around out there. 我已经多次重访,似乎有关于此类问题的错误报告。 Regardless, Exceptions are relatively expensive operations. 无论如何,例外是相对昂贵的操作。 You can use something like the following to circumvent your problem and cut down on cycles. 您可以使用类似以下的方法来避免问题并减少周期。

data1 = None
data2 = None
data3 = None
if filename1 != '':
    data1 = ...
if filename2 != '':
    data2 = ...
if filename3 != '':
    data3 = ...

if data1 is not None:
    if Ynew in data1:
        axarr[1,0].psd(data1[Ynew] ...
    elif Yold in data1:
        axarr[1,0].psd(data1[Yold] ...
if data2 is not None:
    if Ynew in data2:
        axarr[1,0].psd(data2[Ynew] ...
    elif Yold in data2:
        axarr[1,0].psd(data2[Yold] ...
if data3 is not None:
    if Ynew in data3:
        axarr[1,0].psd(data3[Ynew] ...
    elif Yold in data3:
        axarr[1,0].psd(data3[Yold] ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM