Advertisement
tomleo

to CSV or to Pickle

Mar 25th, 2014
488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. import os
  2. import csv
  3. import pickle
  4.  
  5. data = [chr(i) for i in range(65, 65+26)]
  6. pickled_data = pickle.dumps(data)
  7. csv_data = ','.join(data)
  8.  
  9. def test_pickle():
  10.    
  11.     with open('sample_data.pkl', 'w') as f:
  12.         f.write(pickled_data)
  13.  
  14.     loaded_data = []
  15.     with open('sample_data.pkl', 'r') as f:
  16.         loaded_data.extend(pickle.loads(f.read()))
  17.  
  18. def test_csv():
  19.  
  20.     with open('sample_data.csv', 'w') as f:
  21.         csv_writer = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC)
  22.         csv_writer.writerow(data)
  23.  
  24.     loaded_data = []
  25.     with open('sample_data.csv', 'r') as f:
  26.         csv_reader = csv.reader(f)
  27.         loaded_data.extend( [row for row in csv_reader] )
  28.  
  29. if __name__ == '__main__':
  30.     import timeit
  31.     print("Pickling Time: %s" % timeit.timeit("test_pickle()", setup="from __main__ import test_pickle", number=10000))
  32.     print("CSV Timei: %s" % timeit.timeit("test_csv()", setup="from __main__ import test_csv", number=10000))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement