Python尝试除外

Python尝试除外,python,try-except,Python,Try Except,我会包括任务的描述,这个代码应该做的情况下,有人需要它来回答我 #Write a function called "load_file" that accepts one #parameter: a filename. The function should open the #file and return the contents.# # # - If the contents of the file can be interpreted as # an integer, return

我会包括任务的描述,这个代码应该做的情况下,有人需要它来回答我

#Write a function called "load_file" that accepts one 
#parameter: a filename. The function should open the
#file and return the contents.#
#
# - If the contents of the file can be interpreted as
#   an integer, return the contents as an integer.
# - Otherwise, if the contents of the file can be
#   interpreted as a float, return the contents as a
#   float.
# - Otherwise, return the contents of the file as a
#   string.
#
#You may assume that the file has only one line.
#
#Hints:
#
# - Don't forget to close the file when you're done!
# - Remember, anything you read from a file is
#   initially interpreted as a string.


#Write your function here!
def load_file(filename):
    file=open(filename, "r")
    try:
        return int(file.readline())
    except ValueError:
        return float(file.readline())
    except:
        return str(file.readline())
    finally:
        file.close()


#Below are some lines of code that will test your function.
#You can change the value of the variable(s) to test your
#function with different inputs.
#
#If your function works correctly, this will originally
#print 123, followed by <class 'int'>.
contents = load_file("LoadFromFileInput.txt")
print(contents)
print(type(contents))
所以我猜除了语句之外,错误发生在第一个
语句中,但我不明白为什么。如果在将文件中的值转换为浮点值时发生错误,代码是否应转到除
语句之外的第二个
?在第二个
中,除了
之外,它将被转换为字符串,不管怎样它都可以工作?我猜我误解了
try-except(指定错误)-except(无指定错误)
的工作原理


抱歉发了这么长的帖子。

否,除了
块之外,将只执行这些块中的一个,即与异常匹配的第一个块。您所描述的行为将对应于

except ValueError:
    try:
        return float(file.readline())
    except:
        return str(file.readline())
代码不应该转到第二个EXPECT语句吗

否:此“平坦”try/except语句仅适用于第一个
try
块。如果出现异常,则
except
分支将捕获该异常并立即计算相应的块。如果在这个块中发生异常,它不会被任何东西捕获,因为那里没有
try

因此,您必须执行大量嵌套的try/except语句:

try:
    do_this()
except ValueError:
    try:
        do_that()
    except ValueError:
        do_this()
    except:
        do_that_one()
except:
    # a whole bunch of try/except here as well
您可能需要添加额外的嵌套级别

就您需要编写的代码量而言,这是非常低效的。更好的选择可能是:

data = file.readline()
for converter in (int, float, str):
    try:
        return converter(data)
    except:
        pass

请注意,如果执行
converter(file.readline())
,则在每次迭代中(或者在您的情况下,在任何新的try/except块中)都将读取新行,这可能不是您所需要的。

此外,如果在
try:
块中发生错误,则该行已被读取,并且下次
readline()将被删除
将被调用,它将读取文件中的下一行,因此您的
除外:
代码不会在与失败代码相同的值上运行,而是在文件中的下一行上运行。可能将读取的行放在一个var中,并在
try
except
块中返回var的强制转换。这是发生的下一个问题,请根据您的建议解决它。谢谢
def load_file(filename):
    file=open(filename, "r")
    try:
        val = file.readline()
        return int(val)
    except ValueError:
        try:
            return float(val)
        except:
            return str(val)
    finally:
        file.close()
def load_file(filename):
    file=open(filename, "r")
    try:
        val = file.readline()
        return int(val)
    except ValueError:
        try:
            return float(val)
        except:
            return str(val)
    finally:
        file.close()