Dictionary is used to store data values in the form of key:value pairs. In this article we will see how to write dictionary into a file. Actually we can only write a string to a file. If we want to write a dictionary object, we either need to convert it into string using json or serialize it. Method:1 Storing Dictionary With Object Using Json Approach:
Code: Python3
A dictionary in Python is a collection where every value is mapped to a key. They are unordered, mutable and there is no constraint on the data type of values and keys stored in the dictionary. This makes it tricky for dictionaries to be stored as files. Know more about dictionaries here. Syntax: dictionary = {'geek': 1, 'supergeek': True, 4: 'geeky'} Saving Dictionary to a FileThere are two main approaches for saving dictionaries into files using Python. 1. Text Files The most basic way to save dictionaries in Python would be to store them as strings in text files. This method would include the following steps:
filehandler = open(filename, 'wt') data = str(dictionary) filehander.write(data) Reading from the dictionary back from the stored text files is cumbersome and this method should only be used for smaller and non-critical programs. 2. Pickle Module (Recommended) The pickle module in Python is mostly used in fields like Data Science where data persistence is critical. The pickle module stores the given data as a serialized byte sequence into files which can be easily retrieved at a later time. Pickle module supports various Python objects and dictionaries are one among them. This method would include the following steps:
filehandler = open(filename, 'wb') pickle.dump(dictionary, filehandler) Below is the implementation of the above methods. Example 1: Writing to Text File Python3
filehandler = open(filename, 'wt') data = str(dictionary) filehander.write(data)0 : filehandler = open(filename, 'wt') data = str(dictionary) filehander.write(data)2 , filehandler = open(filename, 'wt') data = str(dictionary) filehander.write(data)4 : filehandler = open(filename, 'wt') data = str(dictionary) filehander.write(data)6 filehandler = open(filename, 'wt') data = str(dictionary) filehander.write(data)7 This article helps you to get to understand three important methods of saving the dictionary in Python. What is dictionary? Dictionary in Python is an unordered collection of data values, used to store data values like a map. It holds ‘key’, ‘value’ pairs. simple dictionary is representation: {‘key1’:’value1’,….} empty dictionary in python is {} or dict() for example, let’s take the following dictionary to illustrate how to save it to a file: a_dict = {'clk': 'click','cm': 'call me','cmb': 'call me back'} keys: a_dict.keys() dict_keys(['clk', 'cm', 'cmb']) Using the save() function of numpy
Using the dump() function of pickle
Using the dump() function of json
Reading the saved files respectivelyread the .npy file
read the .pkl file
read the .json file
Well, i believe that you learnt saving the dictionary to a file using these different methods and reading accordingly. |