파이썬 문자열 인코딩() 메서드
예시
UTF-8은 문자열을 인코딩합니다.
txt = "My name is Ståle"
x = txt.encode()
print(x)
정의 및 사용
메서드는 지정된 encode()
인코딩을 사용하여 문자열을 인코딩합니다. 인코딩을 지정하지 않으면 UTF-8이 사용됩니다.
통사론
string.encode(encoding=encoding, errors=errors)
매개변수 값
Parameter | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
encoding | Optional. A String specifying the encoding to use. Default is UTF-8 | ||||||||||||
errors | Optional. A String specifying the error method. Legal values are:
|
더 많은 예
예시
이 예에서는 ASCII 인코딩과 인코딩할 수 없는 문자를 사용하여 다른 오류가 있는 결과를 보여줍니다.
txt = "My name is Ståle"
print(txt.encode(encoding="ascii",errors="backslashreplace"))
print(txt.encode(encoding="ascii",errors="ignore"))
print(txt.encode(encoding="ascii",errors="namereplace"))
print(txt.encode(encoding="ascii",errors="replace"))
print(txt.encode(encoding="ascii",errors="xmlcharrefreplace"))