python str、list、dict、json之间相互转换


str && list

  • str ===> list
string = 'I’m a str'

# 1. 整体转换
lst = string.split('任何字符串中没有的分割单位,不能为空')
print(lst)
# output : ['I’m a str']

# 2. 分割转换
# 通过split默认属性分割
lst = string.split()
print(lst)
# output : ['I’m', 'a', 'str']

# 3. 强制类型转换
lst = list(string)
print(lst)
# output : ['I', '’', 'm', ' ', 'a', ' ', 's', 't', 'r']
  • list ===> str
lst = ['I’m', 'a', 'str']

# 1. 遍历拼接
new_Str = lst[0] + lst[1] + lst[2]
print(new_Str)
# output : I’mastr

# 2. .join
print(''.join(lst))
print(','.join(lst))
# output : I’mastr
# output : I’m,a,str

str && dict

  • str ===> dict
# 1. json

# 需要注意字符串中的json格式
# json 语法规定 数组或对象之中的字符串必须使用双引号,不能使用单引号
string1 = "{'nonce': 1589174926391, 'number': '111222333', 'prefix': 86}"
string2 = '{"nonce": "1589174926391", "number": "111222333", "prefix": "86"}'

# dct = json.loads(string1)
# print(type(dct), ':', dct)
# error : json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

dct = json.loads(string2)
print(type(dct), ':', dct)
# output : <class 'dict'> : {'nonce': '1589174926391', 'number': '111222333', 'prefix': '86'}

# 2. eval(eval 存在安全性的问题)
dct = eval(string1)
print(type(dct), ':', dct)
# output : <class 'dict'> : {'nonce': 1589174926391, 'number': '111222333', 'prefix': 86}

dct = eval(string2)
print(type(dct), ':', dct)
# output : <class 'dict'> : {'nonce': '1589174926391', 'number': '111222333', 'prefix': '86'}

# 3. literal_eval(推荐)
import ast

dct = ast.literal_eval(string1)
print(type(dct), ':', dct)
# output : <class 'dict'> : {'nonce': 1589174926391, 'number': '111222333', 'prefix': 86}

dct = ast.literal_eval(string2)
print(type(dct), ':', dct)
# output : <class 'dict'> : {'nonce': '1589174926391', 'number': '111222333', 'prefix': '86'}
  • dict ===> str
# 1. 强制类型转换
dct1 = {'nonce': 1589174926391, 'number': '111222333', 'prefix': 86}
dct2 = {'nonce': '1589174926391', 'number': '111222333', 'prefix': '86'}

newString = str(dct1)
print(type(newString), ':', newString)
# output : <class 'str'> : {'nonce': '1589174926391', 'number': '111222333', 'prefix': '86'}

# 2. json.dumps()
newString = json.dumps(dct1)
print(type(newString), ':', newString)
# output : <class 'str'> : <class 'str'> : {"nonce": 1589174926391, "number": "111222333", "prefix": 86}

str && json

  • str ===> json
string1 = "{'nonce': 1589174926391, 'number': '111222333', 'prefix': 86}"
string2 = '{"nonce": "1589174926391", "number": "111222333", "prefix": "86"}'

# jsn = json.loads(string1)
# print(type(jsn), ':', jsn)
# error : json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

jsn = json.loads(string2)
print(type(jsn), ':', jsn)
# output : <class 'dict'> : {'nonce': '1589174926391', 'number': '111222333', 'prefix': '86'}
  • json ===> str
dct1 = {'nonce': 1589174926391, 'number': '111222333', 'prefix': 86}

string = json.dumps(dct1)
print(type(string), ':', string)
# output : <class 'str'> : {"nonce": "1589174926391", "number": "111222333", "prefix": "86"}

list && dict

  • list ===> dict
# 1. zip()
lst1 = ['key1', 'key2', 'key3', 'key4']
lst2 = ['value1', 'value2', 'value3']
dct = dict(zip(lst1, lst2))

print(type(dct), ':', dct)
# output : <class 'dict'> : {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}

# 2. 使用嵌套列表
lst1 = ['key1', 'value1']
lst2 = ['key2', 'value2']
lst3 = ['key3', 'value3']
lst4 = [lst1, lst2, lst3]
dct = dict(lst4)

print(type(dct), ':', dct)
# output : <class 'dict'> : {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
  • dict ===> list
dct = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}

lst = list(dct)
print(type(lst), ':', lst)
# output : <class 'list'> : ['key1', 'key2', 'key3']

lst = list(dct.keys())
print(type(lst), ':', lst)
# output : <class 'list'> : ['key1', 'key2', 'key3']

lst = list(dct.values())
print(type(lst), ':', lst)
# output : <class 'list'> : ['value1', 'value2', 'value3']

dict && json

  • dict字典转json数据
dct = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
jsn = json.dumps(dct)

print(type(jsn), ':', jsn)
# output : <class 'str'> : {"key1": "value1", "key2": "value2", "key3": "value3"}
  • json数据转成dict字典
string = '{"key1": "value1", "key2": "value2", "key3": "value3"}'
dct = json.loads(string)

print(type(dct), ':', dct)
# output : <class 'dict'> : {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM