python save file method summary

  • 2020-11-25 07:21:21
  • OfStack

1 > Save as base 2 file in pkl format


import pickle
pickle.dump(data,open('file_path','wb')) # The suffix .pkl You can add it or not 

If the file is too big


pickle.dump(data,open('file_path', 'wb'),protocol=4)

Read the file:


data= pickle.load(open('file_path','rb'))

2 > Save as base 2 file in npz format


import numpy as np
np.savez('file_path/file_name.npz', data1=X,data2=y)

Read the file:


with np.load('file_path/file_name.npz') as data:
X = data['data1']
y= data['data2']

3 > The DataFrame file is saved as.csv


dataframe_file.to_csv("file_path/file_name.csv", index=False)

Read the file:


import pandas as pd
df = pd.read_csv('file_path/file_name.csv')

conclusion


Related articles: