python中字典轉字串單引號變雙引號

2022-09-20 22:42:15 字數 1001 閱讀 5642

兩種方法:str()以及json.dumps()

注意:單引號雙引號的區別

str方法將其變為單引號,json.dumps方法仍是雙引號!!!!

import

json

d=print

(type(d))

str_d=str(d)

print("

str_d:

",str_d)

print("

str_d的型別

",type(str_d))

json_d=json.dumps(d)

print("

json_d:

",json_d)

print("

json_d的型別

",type(json_d))

####################

'dict

'>str_d:

str_d的型別

'str

'>json_d:

json_d的型別

'str

'>

d=

print

(type(d))

str_d=str(d)

print("

str_d:

",str_d)

print("

str_d的型別

",type(str_d))

json_d=json.dumps(d)

print("

json_d:

",json_d)

print("

json_d的型別

",type(json_d))

######################

'dict

'>str_d:

str_d的型別

'str

'>json_d:

json_d的型別

'str

'>

python字串中的單雙引

python中字串可以 且僅可以 使用成對的單引號 雙引號 三個雙引號 文件字串 包圍 this is a book this is a book this is a book 可在單引號包圍的字串中包含雙引號,三引號等,但不能包含單引號自身 需轉義 this is a book this is a...

python 字串轉字典的方法

eval方法轉換字典 user info 使用 eval 卻存在安全性的問題 user eval user info print eval方式 n type user user import json 由於 json 語法規定 陣列或物件之中的字串必須使用雙引號,不能使用單引號 user info1...

Python 字串轉字典(多種方法)

在工作中遇到乙個小問題,需要將乙個python的字串轉為字典,比如字串 user info 我們想把它轉為下面的字典 user dict 有以下幾種方法 1 通過 json 來轉換 import json user info user dict json.loads user info user d...