python-save data

import pickle

data1 = [1,2,3,4]
with open("data1.txt", "w") as data1_file:
    print(data1, file=data1_file, end='')

with open("data1.txt") as data1_file:
    print(data1_file.read())

with open('mydata.pickle', 'wb') as mysavedata:
    pickle.dump([1,2,'three'], mysavedata)

with open('mydata.pickle', 'rb') as myresttoredata:
    a_list = pickle.load(myresttoredata)

print(a_list)


pickle是python的一个标准库,可以保存数据到本地
with会自动打开,关闭文件,即使发生异常也能正常关闭,方便。

你可能感兴趣的:(python)