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