最初の行がヘッダーである csv ファイルを解析しています。日付に従って金額列を合計したいのですが、エラー メッセージが表示されます。デバッグするために、エラーメッセージに従って列が数字であるかどうかと文字列であるかどうかを確認しています-そしてそれは両方です。この理由は何でしょうか?
def parseDataFromFile(self,f):
fh = open(f,'r')
s = 0
for line in fh:
#parsing the line according to comma and stripping the '\n' char
year,month,day,amount = line.strip('\n').split(',')
#checking the header row, could check if was first row as well - would be faster
if (amount == "Amount"): continue
#just for the debug checks
#here is the question
if isinstance(amount,str):
print "amount is a string"
#continue
if amount.isdigit:
print "amount is a digit"
#sum on the amount column
s = s + amount
出力: 金額は文字列です 金額は数字です 金額は文字列です 金額は数字です
エラー:
s = s + amount
TypeError: unsupported operand type(s) for +: 'int' and 'str'