我正在解析一个 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
输出:amount是一个字符串amount是一个数字amount是一个字符串amount是一个数字
错误:
s = s + amount
TypeError: unsupported operand type(s) for +: 'int' and 'str'