Cara menggunakan python save load dict

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:

  • Import Json
  • Create A Dictionary  in-order pass it into text file.
  • Open file in write mode.
  • Use json.dumps() for json string

Code:

Python3




import json

  

details= {'Name':"Bob",json1 :json3:5

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 File

There 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:

  • Opening a file in write/append text mode
  • Converting the dictionary into a string
  • Entering the converted string into the file using write function
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:

  • Importing the pickle module
  • Opening the file in write/append binary mode
  • Entering the data into the file using pickle module’s dump method
filehandler = open(filename, 'wb')
pickle.dump(dictionary, filehandler)

Below is the implementation of the above methods.

Example 1: Writing to Text File

Python3




dictionary= {'geek':1,

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.

source: https://www.i2tutorials.com/what-are-dictionaries-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

import numpy as np
np.save(“a_dict.npy”, a_dict)

Using the dump() function of pickle

import pickle
with open(“a_dict.pkl”, “wb”) as f:
pickle.dump(a_dict,f)
f.close()

Using the dump() function of json

import json
with open(“a_dict.json”, “wb”) as t:
json.dump(a_dict,t)
f.close()

Reading the saved files respectively

read the .npy file

new_dict = np.load(‘a_dict.npy’, allow_pickle=’TRUE’)

read the .pkl file

with open(“a_dict.pkl”, “rb”) as f:
new_dict = pickle.load(f)
print(new_dict)

read the .json file

with open(“a_dictionary.json”, “rb”) as f:
new_dict = json.load(f)
print(new_dict)

Well, i believe that you learnt saving the dictionary to a file using these different methods and reading accordingly.