공부일지

[Python]TypeError: expected string or bytes-like object, got 'NoneType' 본문

Computer/Trouble Shooting

[Python]TypeError: expected string or bytes-like object, got 'NoneType'

이르리의 공부일지 2023. 9. 13. 15:38

찾아보니 정규 표현식 쓸 때 자주 발생하는 오류라 한다.

 

정규 표현식은 str 타입에 적용되는데 그게 아닐 경우에 발생하는 문제이므로

 

str로 변환해주면 된다. (해결 방법: 함수 str() 적용)

 

 

 

참고 블로그

https://nyoungsom.tistory.com/62

 

[Python, 에러] TypeError: expected string or bytes-like object

import re def clean(x): x = re.sub("\&\w*\;", "",str(x)) df['contents'].apply(lambda x: clean(x)) 정규표현식을 사용해서 문자 제거 도중 타입에러(TypeError) 발생 ! 변경 전 re.sub("\&\w*\;", "",x) 변경 후 re.sub("\&\w*\;", "",str(x

nyoungsom.tistory.com

 

 


 

그런데 나의 경우 이 문제가 아니었다.

 

해당 오류 메시지 전문을 살펴봤을 때 크롤러 내 특정 키워드(예시. if re.findall(“\(School\)”, “”, a)에서 ‘School’와 같은)

가 있길래 그 단어를 기준으로 찾아봤다.

 

그런데 그 키워드를 갖는 어떤 코드문장 끝에 조용히 오타가 끼어있었다.

 

코드를 돌아보다가 모르는 새에 눌린 알파벳인 것 같다.