如果你已經使用Python,和你的下一個計劃的路線也用Python編寫的加載數據,您可以簡單地使用pickle
模塊,像這樣:
big_list_list = [["A", 1, 520], ["A", 2, 180], ["B", 1, 49]]
import pickle
# Storing the data
with open("data.pickle", "wb") as outfile:
pickle.dump(big_list_list, outfile)
# Retrieving the data
with with open("data.pickle", "rb") as infile:
reconstructed_big_list_list = pickle.load(infile)
這有兩個注意事項:如果部分工作流程包含非Python程序,他們將無法閱讀泡菜。而且你不應該信任來自任意源的pickle文件,因爲它們可能包含惡意代碼。
除了使用泡菜外,您還可以使用JSON文件。在上面的收件人中用json
簡單地替換單詞pickle
。 JSON的優點是許多非Python程序都可以處理它。
更普遍的是使用CSV文件,像這樣:
import csv
with open('data.csv', 'w', newline='') as outfile:
writer = csv.writer(outfile)
writer.writerows(big_list_list)
with open('data.csv', newline='') as infile:
reader = csv.reader(infile)
reconstructed_big_list_list = [row for row in reader]
Python的標準庫中還包括模塊sqlite3
,它允許您將數據寫入到數據庫中,這可能是如果有用的話你的數據變得比簡單的列表清單更復雜,或者你需要併發訪問。
PS:我剛纔看到你注意到你的文件可能很大。在這種情況下,您可以修改CSV解決方案以增量存儲和加載數據:
import csv
with open('data.csv', 'w', newline='') as outfile:
writer = csv.writer(outfile)
for row in big_list_list:
writer.writerow(row)
with open('data.csv', newline='') as infile:
reader = csv.reader(infile)
for row in reader:
print(row)
來源
2016-10-18 10:57:19
Jan
查看'pickle'模塊 – techydesigner