Tuesday 10 September 2019

how to export list into a file and import it again? use pickle

You can use pickle module for that. This module have two methods,
  1. Pickling(dump): Convert Python objects into string representation.
  2. Unpickling(load): Retrieving original objects from stored string representstion. 

import pickle
l = [1,2,3,4]
with open("test.txt", "wb") as fp: #Pickling
    pickle.dump(l, fp)
 

with open("test.txt", "rb") as fp: # Unpickling
    b = pickle.load(fp)
b
 
[1, 2, 3, 4]

No comments:

Post a Comment