Python save dictionary
Storing Dictionaries in Python
What is a Dictionary?
In Python, a dictionary is an unordered, mutable data type consisting of a series of key-value pairs, each consisting of a key and a corresponding value. Keys in a dictionary must be unique, while values can be any type of object, such as strings, numbers, lists, and tuples.
Here is a simple dictionary example:
student = {'name': 'Tom', 'age': 18, 'gender': 'male'}
We can use keys to access values in a dictionary:
print(student['name']) # Outputs 'Tom'
Saving a Dictionary to a File
In Python, we can use the pickle module to save a dictionary to a file. Pickle is Python’s built-in serialization module that converts Python objects into byte streams or files and can reconstruct the same object when needed.
The following is sample code for saving a dictionary to a file:
import pickle
# Create a dictionary
student = {'name': 'Tom', 'age': 18, 'gender': 'male'}
# Open the file in binary write mode
with open('student.pickle', 'wb') as f:
# Write the dictionary to the file
pickle.dump(student, f)
In the code above, we use the pickle.dump() function to write the dictionary to the specified file and then use the with statement to open the file. Note that the file must be opened in binary write mode (‘wb’) to write the dictionary object to the file.
Reading a Dictionary from a File
Reading a dictionary from a file is also simple. We can use the pickle.load() function to read Python objects from a file.
The following is example code for reading a dictionary from a file:
import pickle
# Open the file in binary reading mode
with open('student.pickle', 'rb') as f:
# Read a dictionary object from the file
student = pickle.load(f)
print(student) # Output {'name': 'Tom', 'age': 18, 'gender': 'male'}
In the above code, we use the pickle.load() function to read a Python object from a file. Again, we need to open the file in binary reading mode (‘rb’).
Saving a Dictionary in JSON Format
In addition to the pickle module, we can also use the JSON format to save dictionaries to files. JSON is a lightweight, text-based data interchange format that is highly readable and maintainable, and is widely used in web development.
The following is sample code for saving a dictionary in JSON format:
import json
# Create a dictionary
student = {'name': 'Tom', 'age': 18, 'gender': 'male'}
# Open the file in write mode
with open('student.json', 'w') as f:
# Convert the dictionary to a JSON string and write it to the file
json.dump(student, f)
In the code above, we use the json.dump() function to convert the dictionary object to a JSON string and write it to the file.
Reading a Dictionary from a JSON File
Reading a dictionary from a JSON file is just as easy. We can use the json.load() function to read the JSON string from the file and convert it to a Python object.
The following is sample code for reading a dictionary from a JSON file:
import json
# Open the file in read mode
with open('student.json', 'r') as f:
# Read the JSON string from the file and convert it to a Python object
student = json.load(f)
print(student) # Output {'name': 'Tom', 'age': 18, 'gender': 'male'}
In the above code, we use the json.load() function to read a JSON string from a file and convert it to a Python object.
Conclusion
In Python, we can use either pickle or JSON to save dictionaries to files. The pickle module supports saving any Python object, but it is limited to Python internal use and cannot be used with other languages. JSON, on the other hand, offers advantages in cross-language interoperability. We can choose different methods to save and read dictionary data according to actual needs.