ValueError: invalid literal for int() with base 10
파이썬 형변환에서 발생하는 오류입니다.
우리는 보통 형 변환시에는 하기 함수를 사용합니다.
문자형으로 바꿀 때는 str()
정수형으로 바꿀 때는 int()
실수형으로 바꿀 때는 float()
문자열을 정수로 변환
>>> a = '10'
>>> int(a)
10
문자열을 실수로 변환
>>> b = '4.3'
>>> float(b)
4.3
정수를 문자열로 변환
>>> c = 7
>>> str(c)
'7'
실수를 문자열로 변환
>>> d = 2.71
>>> str(d)
'2.71'
문제 상황
하지만, 여기서 문제 상황은 조금 다릅니다.
>>> d = '51,800'
>>> int(d)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '51,800'
파이썬에서 다음 라인에서 에러가 걸렸습니다.
self.currentPrice = int(thirdColumnInRow.text())
thirdColumnInRow.text() 의 value가 '51,800'이기 때문에 바로 int()함수를 사용할 수 없기 때문입니다.
콤마(',')를 제거한 후 int()함수를 사용해야 합니다.
해결 방법
currentPrice = thirdColumnInRow.text().strip().replace(',', '')
self.currentPrice = int(currentPrice)
이렇게 로직을 수정하니 깔끔하게 해결되었습니다.