IT-개발,DB

python 문자열 변환

SB리치퍼슨 2021. 2. 25. 09:01

python 문자열 변환

type string to bytes

type bytes to string

type string to dict

type string to list  <-- ast.literal_eval() 와 json.loads()함수 2가지 방법이 있습니다.

# bytes type to unicode string type
import json
import ast

str_text = "{\"key\": \"astrg\"}"
# type string to bytes
bytes_text = str_text.encode()
print(bytes_text, type(bytes_text))
# type bytes to string
str = bytes_text.decode()
print(str, type(str))
# string to dict
dict_text = json.loads(str)
print(dict_text, type(dict_text))
# type string to list
x1 = "['B-EXP', 'I-EXP', 'B-SUB', 'I-SUB', 'O']"
x2 = ast.literal_eval(x1)
print(x2, type(x2))
# type string to list
#x2 = json.loads(x1)
#print(x2, type(x2))

반응형