我遇到了一個問題「int()與基數10的無效文字:''」,這意味着python無法將「'轉換爲整數。但是我已經添加了一個條件if(sh.cell_value(rowx=rx, colx=3)!=''):
,它可以跳過不包含任何內容的塊。有沒有人有一些想法?非常感謝你!int()與基數爲10的無效字面值:''
import xlrd
book = xlrd.open_workbook("streeteasy.xls")
sh = book.sheet_by_index(0)
total = 0
for rx in range (sh.nrows):
if(sh.cell_value(rowx=rx, colx=3)!=''):
s = sh.cell_value(rowx=rx, colx=3)
print filter(unicode.isdigit, s)
print int(filter(unicode.isdigit, s))
total += int(filter(unicode.isdigit, s))
如果您引用的錯誤消息是實際錯誤消息,則該字符串不爲空。它包含一個空間。另外,即使你的if語句塊沒有執行,你仍然繼續循環的其餘部分,並嘗試在's'上調用'int'。 – BrenBarn
檢查'如果string.strip()',這將刪除與空格... –
@BrenBarn除非發佈後忍者編輯後,你的評論,我認爲你誤讀。 ''''是空字符串 - 沒有空格。 – sepp2k