>>> n = ''.join(i for i in x if i.isdigit())
>>> n.isdigit()
True
>>> x.isdigit()
False
>>> previous = 0
>>> next = 100
>>> answer = 0
>>> for i in range(0,100):
... answer += int(n[previous:next])
... previous = next
... next += 100
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ValueError: invalid literal for int() with base 10: ''
为什么我会收到这个错误?如您所见,n 是数字..
最佳答案
n
可能是数字,但在某个阶段你会超过 n
的长度,这样 n[previous:next]
根本不包含任何字符。空字符串 ''
无法转换为 int,因此会出现说明完整情况的错误:invalid literal for int() with base 10: ''
.
>>> int('')
Traceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''
关于python - 基数为 10 的 int 的无效文字 : '' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4484728/