ValueError:int() 的无效文字以 10 为基数: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)

但我收到以下错误。

ValueError: invalid literal for int() with base 10: ''

这里有什么明显的东西我遗漏了吗?

乔恩·基帕斯基

我猜文件末尾有一个空行。这在剥离时变成空字符串,无法解析为 int。

删除空行将使这项工作生效,但您可以将其视为防御性编程中的练习。正如我最喜欢的一位教授曾经告诉我的那样,“一个好的程序员是那种在过单向街之前会看两边的人”。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

ValueError:对于基数为 10 的 int() 无效文字:Python/IndexError 中的 '':列表索引超出范围

ValueError: 基数为 10 的 int 的无效文字:' '

ValueError:以 10 为基数的 int() 的文字无效?

ValueError:int() 的无效文字以 10 为基数:'['

*** ValueError:int() 的无效文字,基数为 10:“#”

ValueError:int() 的无效文字以 10 为基数:':'

ValueError:int() 的无效文字,基数为 10:' '

Python的int()无效文字,基数为10:“。”

基数为 2 的 int() 的 ValueError 无效文字:Python 中的 ''

返回int(value)ValueError:以10为基数的int()的无效文字

ValueError:int() 的无效文字,基数为 10:'pippi'

pandas ValueError:以10为基数的int()的无效文字

speedtest-cli:ValueError:int() 的无效文字,基数为 10:''

显示 ValueError: 以 10 为基数的 int() 的无效文字:'\n'

ValueError:int() 的无效文字,基数为 10:'E'

ValueError:int() 的无效文字以 10 为基数:'' 在输入整数期间

ValueError:以10为基数的int()的无效文字如何修复错误

ValueError:以10为基数的int()的无效文字:'Home'

ValueError: 基数为 10 的 int() 的文字无效:'\r'

Jupyter Notebook - ValueError: 以 10 为基数的 int() 的无效文字:''

LeetCode 反向整数(ValueError:int() 的无效文字,基数为 10: '' )

Python为基数为10的int()输入无效的文字

ValueError:int() 的无效文字,基数为 10:'' from Python 2.X to 3.X

基数为 10 的 int() 的 Python 无效文字:“开始”

Python:以 10 为基数的 int() 错误无效文字

基数为 10 的 int() 的无效文字:'PORT'

基数为 10 的 int() 的无效文字:-django

基数为 10 的 int() 的无效文字:'string'

基数为 10 的 int() 的无效文字:'1044.0'