python變量數據之保存

字典數據dict

1 使用yaml保存

參考:http://www.cocoachina.com/articles/92667

# yaml安裝
conda install -c anaconda pyyaml         

保存

import yaml
data = {
    'key1': 1,
    1: 2
}
with open('data.yml', 'w') as outfile:
    yaml.dump(data, outfile, default_flow_style=False)

讀取

with open("data.yml", 'r') as stream:
    data = yaml.load(stream)
    print data[1] # 2

2 使用numpy保存

參考:https://blog.csdn.net/yangtf07/article/details/81571371

import numpy as np
# Save
dictionary = {'hello':'world'}
np.save('my_file.npy', dictionary) 
# Load
read_dictionary = np.load('my_file.npy').item()
print(read_dictionary['hello']) # displays "world"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章