Skip to content

Instantly share code, notes, and snippets.

@atul10595
Last active October 22, 2019 08:09
Show Gist options
  • Save atul10595/5eb7824aba7fa9371b401142c853b077 to your computer and use it in GitHub Desktop.
Save atul10595/5eb7824aba7fa9371b401142c853b077 to your computer and use it in GitHub Desktop.
read pickle file, python
import pickle
from server.modules.logging.logger import log_error
def read_file(filename):
# file = 'static/data/pickle/r_data_20.pkl'
file = filename
data = None
with (open(file, "rb")) as openfile:
data = pickle.load(openfile)
if data is None:
print "data is not read"
return None
print "database file read."
return data
# storing data in a file
def export_file(filename, data):
# filename = 'static/data/pickle/r_data_' + str(data[0]) + '.pkl'
print 'in export_file'
try:
filename = filename.encode('utf-8') #encode because maybe mongodb returns u'abcdef' string
pickle_out = open(filename, 'w+')
pickle.dump(data, pickle_out, pickle.HIGHEST_PROTOCOL)
pickle_out.close()
print "Data stored in pickle file: {}".format(filename)
return 1
except Exception as err:
print 'Error storing file'
log_error(err)
return -1
from pickleapi import read_file
data = read_file("static/data/pickle/r_data_20.pkl")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment