BaşlayınÜcretsiz Başlayın

Loading a pickled file

There are a number of datatypes that cannot be saved easily to flat files, such as lists and dictionaries. If you want your files to be human readable, you may want to save them as text files in a clever manner. JSONs, which you will see in a later chapter, are appropriate for Python dictionaries.

However, if you merely want to be able to import them into Python, you can serialize them. All this means is converting the object into a sequence of bytes, or a bytestream.

In this exercise, you'll import the pickle package, open a previously pickled data structure from a file and load it.

Bu egzersiz

Introduction to Importing Data in Python

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

  • Import the pickle package.
  • Complete the second argument of open() so that it is read only for a binary file. This argument will be a string of two letters, one signifying 'read only', the other 'binary'.
  • Pass the correct argument to pickle.load(); it should use the variable that is bound to open.
  • Print the data, d.
  • Print the datatype of d; take your mind back to your previous use of the function type().

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

# Import pickle package


# Open pickle file and load data: d
with open('data.pkl', ____) as file:
    d = pickle.load(____)

# Print d
print(____)

# Print datatype of d
print(____)
Kodu Düzenle ve Çalıştır