gpt4 book ai didi

python - 值错误 : invalid literal for int() with base 10: '' in python code

转载 作者:太空宇宙 更新时间:2023-11-04 07:59:09 25 4
gpt4 key购买 nike

我有以下代码行可以打开一个名为 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。

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

关于python - 值错误 : invalid literal for int() with base 10: '' in python code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43485393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com