:: python
python :: 파이썬 Bytes to String 변환하기
토람이
2021. 11. 18. 23:34
파이썬 subprocess 라이브러리를 이용하여 리눅스 커맨드를 실행한 후
실행 결과 내용물을 받아왔다.
(* 참고: https://toramko.tistory.com/11)
String 형태로 받아올 것을 기대하고 코드를 짰더니
TypeError: must be str, not bytes
TypeError 에러가 났다.
String 이 아닌 Bytes 타입이라니..?
subprocess 실행 후 받아온 결과를 찍어보았다.
b'Hello Toramko.\n'
이렇게 'b' 로 시작되는 형태가 출력되었는데, 이는 String 이 아닌 Bytes 타입이다.
그렇다면 Bytes -> String 으로 변환하려면?
sentence = str(sentence, "utf-8")
요렇게 해주면 된다. 매우 간단!
300x250