rows = []
FILE = open("testing.txt", "r")
for blob in FILE: rows.append([int(i) for i in blob.split(" ")])
這裏testing.txt包含無效字面INT()與基礎文件10只包含整數
01 23 04 05 67
08 09 10
11
12
但是當我運行的代碼,我得到以下錯誤:
ValueError Traceback (most recent call last)
<ipython-input-1-2086c8bf9ab4> in <module>()
1 rows = []
2 FILE = open("testing.txt", "r")
----> 3 for blob in FILE: rows.append([int(i) for i in blob.split(" ")])
ValueError: invalid literal for int() with base 10: ''
所以我的問題是:int()有什麼問題?我認爲如果參數是一個整數是完全可以的(例如int(5) == 5
)。謝謝。
你不應該使用列表(f)或f.readlines()來獲取到文件中的行? – Powertieke
Peter Varo:是的,當我擺脫'\ n'並確保每個連續數字之間只有一個空格時,它就可以工作。但是爲什麼當這個鏈接 中的作者能夠在triangle.txt包含100行時成功? –
triange.txt和 –