콘솔워크

str int 형변환 에러 invalid literal for int() with base 10:'' 본문

프로그래밍/python

str int 형변환 에러 invalid literal for int() with base 10:''

이휘재123 2022. 7. 15. 15:06
반응형

간혹 엑셀파일에서 숫자로 된 문자열을 읽어오면 뒤에 소수점이 붙어버리는 경우가 있다.

ex) 3000.0

 

이런 경우 강제로 형변환을 하게되면 위와 같은 오류가 발생하는데 그런 경우 사용할 수 있다.

def filter_only_digit_remove_dot(text):
    text = str(text)
    if text.find(".") > -1:
        text = text.split(".")[0]
    numbers = re.findall(r'\d+', text)
    return ''.join(numbers)

 

반응형