python:尝试,继续,循环说明



我刚刚了解了围绕For-loops中遇到的错误的概念。我从本地计算机中读取了一系列文件列表,我想将其读为PANDAS DataFrames。

假设我有一个文件列表,每个文件作为列" A"," B"one_answers" C"。如果有特定的列,请说来自file3.tbl的列" b",我的计算机上的文件缺少,我想继续使用我的for循环。

list = ['file1.tbl', 'file2.tbl', 'file3.tbl']
for i in range(len(list)):
    data = pandas.read_csv(list[i])
    try:
        b = data['B']
        continue
    except Exception:
        print "Column B not included in file: ", list[i]

这似乎有些有效,但它打印了统计len(列表)次数的次数,例如:

Column B not included in file: file3.tbl
Column B not included in file: file3.tbl
Column B not included in file: file3.tbl

有没有一种方法可以让它仅在该特定迭代中打印一次?

如评论中所示,您可能有名称空间问题。这是一些清理的代码,应为每个Exception唯一打印。它包括与评论一致的Pythonic建议。

对于三个类似CSV的文件"file1.tbl", "file2.tbl", "file3.tbl",我得到以下内容:

import pandas as pd

filenames = ["file1.tbl", "file2.tbl", "file3.tbl"]        # @John Gordon
for fn in filenames:
    data = pd.read_csv(fn)
    try: 
        b = data['B']
    except (KeyError):                                     # @Ryan
        print("Column B not included in file: ", fn)
    else:
        # Do something with b (optional)
        pass

# Column B not included in file:  file1.tbl
# Column B not included in file:  file2.tbl
# Column B not included in file:  file3.tbl

最新更新