ValueError:int()的无效文字,基数为10:''在python代码中

ros*_*ose 1 python

我有以下代码行打开一个名为document.txt的文档,看起来有点像这样.

3 4 5 6 6

3 2 8 9

4 6

with open('document.txt','r') as f:
        for line in f:
                farray = [int(i) for i in line.split(" ")]
                lArray.append(farray)
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误.

ValueError: invalid literal for int() with base 10: ''
Run Code Online (Sandbox Code Playgroud)

这里有什么明显的东西让我失踪吗?

Jon*_*sky 6

我猜测文件末尾有一个空行.剥离后,这将变为空字符串,无法将其解析为int.

删除空白行将使这项工作成功,但您可以将其视为防御性编程中的练习.正如我最喜欢的教授之一曾经告诉我的那样,"一个优秀的程序员是在穿越单行道之前双向看的人".