字典(dict)轉為字符串(string) 我們可以比較容易的將字典(dict)類型轉為字符串(string)類型。 通過遍歷dict中的所有元素就可以實現字典到字符串的轉換: for key, value in sample_dic.items(): print "\"%s\":\"%s\"" % (key, value) 字符串(string)轉為字典(dict) 如何將一個字符串(string)轉為字典(dict)呢? 其實也很簡單,只要用 eval()或exec() 函數就可以實現了。 >>> a = "{'a': 'hi', 'b': 'there'}" >>> b = eval(a) >>> b {'a': 'hi', 'b': 'there'} >>> exec ("c=" + a) >>> c {'a': 'hi', 'b': 'there'} >>>