我有包含了像蟒蛇:ValueError異常:無效的字面INT()基數爲10:「」
70154::308933::3
UserId::ProductId::Score
進入我寫這個程序來讀取文本文件: (對不起了indendetion是有點亂在這裏)
def generateSyntheticData(fileName):
dataDict = {}
# rowDict = []
innerDict = {}
try:
# for key in range(5):
# count = 0
myFile = open(fileName)
c = 0
#del innerDict[0:len(innerDict)]
for line in myFile:
c += 1
#line = str(line)
n = len(line)
#print 'n: ',n
if n is not 1:
# if c%100 ==0: print "%d: "%c, " entries read so far"
# words = line.replace(' ','_')
words = line.replace('::',' ')
words = words.strip().split()
#print 'userid: ', words[0]
userId = int(words[0]) # i get error here
movieId = int (words[1])
rating =float(words[2])
print "userId: ", userId, " productId: ", movieId," :rating: ", rating
#print words
#words = words.replace('_', ' ')
innerDict = dataDict.setdefault(userId,{})
innerDict[movieId] = rating
dataDict[userId] = (innerDict)
innerDict = {}
except IOError as (errno,strerror):
print "I/O error({0}) :{1} ".format(errno,strerror)
finally:
myFile.close()
print "total ratings read from file",fileName," :%d " %c
return dataDict
,但我得到的錯誤:
ValueError: invalid literal for int() with base 10: ''
有趣的是,它是worki ng只是很好的閱讀相同的格式數據從其他文件.. 其實發布此問題時,我注意到一些奇怪的.. 條目70154 :: 308933 :: 3 每個數字有一個space.in之間像7空間0空間1空間5空間4空間::空間3 ... 該文本文件看起來不錯.. :(複製粘貼只顯示這種性質.. 反正..但任何線索怎麼回事。 感謝
如何閱讀文本文件?發佈您的代碼。 – jozzas