첫 번째 줄이 헤더 인 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'