參考資料:
https://kite.com/python/answers/how-to-save-a-dictionary-to-a-file-in-python
通過如下的代碼,可以將Python中的字典保存到一個(二進制)文件中。當然,這個方法是通用的,調用了pickle這個包,能夠保存Python中所有的對象。
dictionary_data = {"a": 1, "b": 2} a_file = open("data.pkl", "wb") pickle.dump(dictionary_data, a_file) a_file.close() a_file = open("data.pkl", "rb") output = pickle.load(a_file) print(output) ## OUTPUT ## {'a': 1, 'b': 2} a_file.close()