我从字面上刚刚了解了解决for循环中遇到的错误的概念。我有一个从本地计算机读入的文件列表,我想将它们作为pandas数据帧读取。
我们说我有一个文件列表,每个文件都是列" A"," B"和" C"。 如果有一个特定的列,请说列" B"从file3.tbl,我的计算机上的文件中丢失,我想继续我的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(list)次数,如下所示:
Column B not included in file: file3.tbl
Column B not included in file: file3.tbl
Column B not included in file: file3.tbl
有没有办法让它只为特定的迭代打印一次?
答案 0 :(得分:1)
正如评论中暗示的那样,您可能会遇到名称空间问题。这里有一些清理过的代码,应该为每个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