Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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:Try,Continue,for循环中的语句除外_Python_For Loop_Continue_Except - Fatal编程技术网

Python:Try,Continue,for循环中的语句除外

Python:Try,Continue,for循环中的语句除外,python,for-loop,continue,except,Python,For Loop,Continue,Except,实际上,我刚刚了解了处理for循环中遇到的错误的概念。我有一个从本地计算机读入的文件列表,我想把它们作为数据帧读入 假设我有一个文件列表,每个文件都列为“a”、“B”和“C”。 如果我的计算机上的某个文件中缺少一个特定的列,比如file3.tbl中的列“B”,我想继续我的for循环 list = ['file1.tbl', 'file2.tbl', 'file3.tbl'] for i in range(len(list)): data = pandas.read_csv(list[i]

实际上,我刚刚了解了处理for循环中遇到的错误的概念。我有一个从本地计算机读入的文件列表,我想把它们作为数据帧读入

假设我有一个文件列表,每个文件都列为“a”、“B”和“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]
这似乎有些效果,但它会打印except STANTT 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

有没有办法让它在特定的迭代中只打印一次?

正如注释中所暗示的,您可能有名称空间问题。下面是一些经过清理的代码,应该为每个
异常唯一地打印这些代码。它包括同意这些评论的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

正如注释中所暗示的,您可能有名称空间问题。下面是一些经过清理的代码,应该为每个
异常唯一地打印这些代码。它包括同意这些评论的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


你的意思是
list[i]
而不是
list[ii]
?另外,您的
try
块上的缩进已关闭。是的,我关闭了!我将编辑这两个。除了异常,不要执行
;它有可能隐藏一些你更想知道的事情<代码>除KeyError外
更为具体。还有,你确定这是你的代码吗?当列表只包含
.tbl
时,打印
.dat
是很奇怪的。两个样式指针:不要对变量使用内置名称(例如
列表
)。使用有意义的名称,如
文件
文件名
。另外,最好直接使用
for file in files:
循环列表中的项目,而不是循环列表索引。我认为@Ryan解决了这个问题——您的错误消息指的是
file3.dat
,但您的代码定义了
file3.tbl
。这不是您正在运行的实际代码。您是指
list[i]
而不是
list[ii]
?另外,您的
try
块上的缩进已关闭。是的,我关闭了!我将编辑这两个。除了异常,不要执行
;它有可能隐藏一些你更想知道的事情<代码>除KeyError外
更为具体。还有,你确定这是你的代码吗?当列表只包含
.tbl
时,打印
.dat
是很奇怪的。两个样式指针:不要对变量使用内置名称(例如
列表
)。使用有意义的名称,如
文件
文件名
。另外,最好直接使用
for file in files:
循环列表中的项目,而不是循环列表索引。我认为@Ryan解决了这个问题——您的错误消息指的是
file3.dat
,但您的代码定义了
file3.tbl
。这不是您正在运行的实际代码。嗨,pylang,谢谢您对建议进行了很好的总结。else语句是继续逻辑的except部分,还是继续try语句停止的地方?还是从循环的开头开始?@DaxFeliz:else语句只有在没有异常发生的情况下才会执行。它在没有异常的情况下从
try
块继续执行,如@Arun所述。逻辑上,这对我来说很有意义,但是这个版本仍然会多次打印except消息。我在
.tbl
文件(?)上运行代码,并在更新后的帖子中获得结果。尝试新的Python会话。然后尝试新的文件。嗨,派朗,谢谢你很好地总结了这些建议。else语句是继续逻辑的except部分,还是继续try语句停止的地方?还是从循环的开头开始?@DaxFeliz:else语句只有在没有异常发生的情况下才会执行。它在没有异常的情况下从
try
块继续执行,如@Arun所述。逻辑上,这对我来说很有意义,但是这个版本仍然会多次打印except消息。我在
.tbl
文件(?)上运行代码,并在更新后的帖子中获得结果。尝试新的Python会话。然后尝试新文件。