Python -> JSON list, tuple -> array ⇡dump, dumps Serialization, Encoding We can say writing JSON file over the Python format |
JSON -> Python array -> list ⇡load, loads Deserialization, Decoding We can say reading JSON file over the Python format |
import json
student_data = {
"1.FirstName": "Gildong",
"2.LastName": "Hong",
"3.Age": 20,
"4.University": "Yonsei University",
"5.Courses": [
{
"Major": "Statistics",
"Classes": ["Probability",
"Generalized Linear Model",
"Categorical Data Analysis"]
},
{
"Minor": "ComputerScience",
"Classes": ["Data Structure",
"Programming",
"Algorithms"]
}
]
}
with open('student_file.json', 'w') as json_file:
json.dump(student_data,json_file)
'w' : Write mode
with A as B : execute A and then save A as B. For example,
with open("student_file.json", "w") : open the "student_file.json" file with write mode
json.dump or jumps : serialize(encoding) the python to json.
dump : save JSON format file in the disk
dumps : using JSON format data in code editor
For example,
json.dump(student_data, json_file) : serialize(encoding) "student_data" python to json_file(will be written in json)
json.dumps(student_data) : using "student_data" data on the code editor continually
data = {
"president": {
"name": "Zaphod Beeblebrox",
"species": "Betelgeusian"
}
}
with open("data_file.json", "w") as write_file:
json.dump(data, write_file)
json.dumps(data)
>>>
{"president": {"name": "Zaphod Beeblebrox", "species": "Betelgeusian"}}
json.dumps(data, indent=4)
>>>
{
"president": {
"name": "Zaphod Beeblebrox",
"species": "Betelgeusian"
}
}
indent : JSON array elements and object members will be pretty-printed with that indent level.
sort_keys : If it is true (default: False), then the output of dictionaries will be sorted by key.
st_json=json.dumps(student_data, indent=4, sort_keys=True)
st_json
>>>
{
"1.FirstName": "Gildong",
"2.LastName": "Hong",
"3.Age": 20,
"4.University": "Yonsei University",
"5.Courses": [
{
"Classes": [
"Probability",
"Generalized Linear Model",
"Categorical Data Analysis"
],
"Major": "Statistics"
},
{
"Classes": [
"Data Structure",
"Programming",
"Algorithms"
],
"Minor": "ComputerScience"
}
]
}
'r' : Read mode
json.load or loads : deserialize(decoding) json to python.
load : save python format file in the disk
loads : using python format data in code editor
For example,
json.load(st_json) : deserialize(decoding) "st_json" json_file to python
json.loads(st_json3) : using "st_json3" data on the code editor continually
with open('student_file.json','r') as st_json:
st_python=json.load(st_json)
st_python
>>>
{'1.FirstName': 'Gildong',
'2.LastName': 'Hong',
'3.Age': 20,
'4.University': 'Yonsei University',
'5.Courses': [{'Classes': ['Probability',
'Generalized Linear Model',
'Categorical Data Analysis'],
'Major': 'Statistics'},
{'Classes': ['Data Structure', 'Programming', 'Algorithms'],
'Minor': 'ComputerScience'}]}
st_python2=json.loads(st_json3)
st_python2
>>>
{'1.FirstName': 'Gildong',
'2.LastName': 'Hong',
'3.Age': 20,
'4.University': 'Yonsei University',
'5.Courses': [{'Classes': ['Probability',
'Generalized Linear Model',
'Categorical Data Analysis'],
'Major': 'Statistics'},
{'Classes': ['Data Structure', 'Programming', 'Algorithms'],
'Minor': 'ComputerScience'}]}
- contextlib
It automate calls close method within the class. So it implements close method when it used with with.
from contextlib import closing
class OpenClose:
def open(self):
print('start the work')
def do_something(self):
print('emplementing the work')
print('emplementing the work')
def close(self):
print('terminate the work')
class doOpenClose():
with closing(OpenClose()) as f:
f.open()
f.do_something()
>>>
start the work
emplementing the work
emplementing the work
terminate the work
TIPS
|
reference
'Basic Python' 카테고리의 다른 글
copy, copy.copy, copy.deepcopy, get, next, id (0) | 2021.05.04 |
---|---|
Module, __name__, __main__ (0) | 2021.04.27 |
Class, pass, instance, binding, Object, variable, self, method, __init__, dir, namespace, __dict__, __del__ (0) | 2021.04.20 |
parameter, argument, def, *arg, **kwargs, return, global, *val, nonlocal, namespace (0) | 2021.01.27 |
try, except, else, finally, sys.stderr, raise (0) | 2020.12.16 |