반응형
/*******************************************************************************************************************
-- Title : [Py3.5] Export/Import python Objects w/ pickle
-- Reference : googling
-- Key word : dump pickle 데이터 덤프 피클 unpickle 언피클 data dump
*******************************************************************************************************************/
■ Scripts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | import pickle import pandas as pd # -- # -- pickling, dump # -- # 파이썬 대부분의 객체를 파일로 export/import 할 수 있다. ######################################## # -- sample objects ######################################## # -- dataframe sample df = pd.DataFrame({"c1":[1,2,3], "c2":[11,22,33], "c3":[111,222,333]}, index=["i1","i2","i3"]) print (df) print("... sample_dataframe", "." * 100, "\n") # -- -- list sample lst = [["a", "b", "c"], "g", ["h", "i"]] print (lst) print("... sample_list", "." * 100, "\n") ######################################## # -- export dump objects ######################################## # -- export dump dataframe with open('df_sample.dmp', 'wb') as fp: pickle.dump(df, fp) # -- export dump list with open('lst_sample.dmp', 'wb') as fp: pickle.dump(lst, fp) ######################################## # -- import dump files ######################################## # -- declare empty objects imp_df = pd.DataFrame() imp_lst = [] # -- import dump to dataframe with open ('df_sample.dmp', 'rb') as fp: imp_df = pickle.load(fp) print(imp_df) print("... imported_dataframe", "." * 100, "\n") # -- import dump to list with open ('lst_sample.dmp', 'rb') as fp: imp_lst = pickle.load(fp) print(imp_lst) print("... imported_list", "." * 100, "\n") | cs |
반응형