我解析一个csv文件,其中第一行是标题。 我想根据日期总结金额栏,但是收到错误消息。 要调试我正在检查列是否是一个数字,以及它是否根据错误消息的字符串 - 它是两个。 这可能是什么原因?python解析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'