This is an introduction to a JSON file that is convenient for summarizing the output results created in Python. As I will explain later, Python lists and dictionary formats can be saved as they are, so Almost no conversion for input / output is required. Also, since import json for input / output is included, it is not necessary to install another library.
First of all, it's painful if you don't know about lists and dictionaries, so I'll write them down. If you say "I know!", Please skip it.
Think of a list as an array in C ++ (my understanding). In Python, the variable is treated as a list when initialized with []. In the example below, the variable a is initialized as a list. In the for statement, the value of i is added to the end of the list. You can access each element by specifying the number in the list in [].
a = [] #Initialize as a list
# 0~Stores values up to 9
for i in range(10):
a.append(i)
print(a)
#output:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#Element reference
print(a[0]) #Output: 0
print(a[1]) #Output: 1
print(a[2]) #Output: 2
A dictionary is like std :: map in C ++, and manages each element by a value paired with a stored element called a key instead of the element number of the list. It's complicated, but if you initialize it with {}, the variable will be treated as a dictionary. In the example below, the variable b is initialized as a dictionary.
b = {} #Initialize as a dictionary
#With j as the key, j*Store value of 10
for j in range(10):
b[j] = j * 10
print(b)
#output:{0: 0, 1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60, 7: 70, 8: 80, 9: 90}
print(b[0]) #0
print(b[1]) #10
print(b[2]) #20
You can also use a string for the key. In addition, you can create a dictionary in the dictionary. However, please note that you cannot use the same key more than once.
ys = {}
ys["honoka"] = {"B": 78, "W": 58, "H": 82}
ys["eri"] = {"B": 88, "W": 60, "H": 84}
ys["kotori"] = {"B": 80, "W": 58, "H": 80}
ys["umi"] = {"B": 76, "W": 58, "H": 80}
ys["rin"] = {"B": 75, "W": 59, "H": 80}
ys["maki"] = {"B": 78, "W": 56, "H": 83}
ys["nozomi"] = {"B": 90, "W": 60, "H": 82}
ys["hanayo"] = {"B": 82, "W": 60, "H": 83}
ys["niko"] = {"B": 74, "W": 57, "H": 79}
print(ys)
#output:{'nozomi': {'H': 82, 'B': 90, 'W': 60}, 'umi': {'H': 80, 'B': 76, 'W': 58}, 'niko': {'H': 79, 'B': 74, 'W': 57}, 'kotori': {'H': 80, 'B': 80, 'W': 58}, 'maki': {'H': 83, 'B': 78, 'W': 56}, 'eri': {'H': 84, 'B': 88, 'W': 60}, 'hanayo': {'H': 83, 'B': 82, 'W': 60}, 'rin': {'H': 80, 'B': 75, 'W': 59}, 'honoka': {'H': 82, 'B': 78, 'W': 58}}
print(ys["rin"]["W"])
#Output: 59
What you should pay attention to in the above example is that the output results are not in the storage order. This is because the Python dictionary does not preserve the order in which it was stored. I think that it doesn't matter the order because it is accessed by the key. Therefore, if you want to keep the storage order, make some adjustments to the initialization.
import collections as cl #Import collections
#ys = {}
#{}Instead of collections.OrderedDict()Initialize with
ys = cl.OrderedDict()
ys["honoka"] = cl.OrderedDict({"B": 78, "W": 58, "H": 82})
ys["eri"] = cl.OrderedDict({"B": 88, "W": 60, "H": 84})
ys["kotori"] = cl.OrderedDict({"B": 80, "W": 58, "H": 80})
ys["umi"] = cl.OrderedDict({"B": 76, "W": 58, "H": 80})
ys["rin"] = cl.OrderedDict({"B": 75, "W": 59, "H": 80})
ys["maki"] = cl.OrderedDict({"B": 78, "W": 56, "H": 83})
ys["nozomi"] = cl.OrderedDict({"B": 90, "W": 60, "H": 82})
ys["hanayo"] = cl.OrderedDict({"B": 82, "W": 60, "H": 83})
ys["niko"] = cl.OrderedDict({"B": 74, "W": 57, "H": 79})
print(ys)
#Output: OrderedDict([('honoka', OrderedDict([('B', 78), ('H', 82), ('W', 58)])), ('eri', OrderedDict([('B', 88), ('H', 84), ('W', 60)])), ('kotori', OrderedDict([('B', 80), ('H', 80), ('W', 58)])), ('umi', OrderedDict([('B', 76), ('H', 80), ('W', 58)])), ('rin', OrderedDict([('B', 75), ('H', 80), ('W', 59)])), ('maki', OrderedDict([('B', 78), ('H', 83), ('W', 56)])), ('nozomi', OrderedDict([('B', 90), ('H', 82), ('W', 60)])), ('hanayo', OrderedDict([('B', 82), ('H', 83), ('W', 60)])), ('niko', OrderedDict([('B', 74), ('H', 79), ('W', 57)]))])
The above collections.OrderedDict () is a function that creates an ordered dictionary. By initializing with this, a dictionary that keeps the input order is completed.
By the way, what is the essential JSON format is the same format as the above dictionary format. The contents of the file are
{(Key 1):(Element 1),(Key 2):(Element 2),…}
Since the format is arranged in the order of, it can be said that the dictionary ≒ JSON format. If you want to handle JSON files in Python, you need to import the library for JSON files.
import json
The following is a sample. I will explain using the following JSON file.
myu_s.json
{
"honoka": {
"BWH": [
78,
58,
82
],
"height": 157
},
"eri": {
"BWH": [
88,
60,
84
],
"height": 162
},
"kotori": {
"BWH": [
80,
58,
80
],
"height": 159
},
"umi": {
"BWH": [
76,
58,
80
],
"height": 159
},
"rin": {
"BWH": [
75,
59,
80
],
"height": 155
},
"maki": {
"BWH": [
78,
56,
83
],
"height": 161
},
"nozomi": {
"BWH": [
90,
60,
82
],
"height": 159
},
"hanayo": {
"BWH": [
82,
60,
83
],
"height": 156
},
"niko": {
"BWH": [
74,
57,
79
],
"height": 154
}
}
import json
def main():
f = open("myu_s.json", 'r')
#Here is important! !!
json_data = json.load(f) #Read in JSON format
name_list = ["honoka","eri","kotori","umi","rin","maki","nozomi","hanayo","niko"]
for name in name_list:
print("{0:6s}height:{1}cm BWH: ".format(name,json_data[name]["height"]),end="\t")
for i in range(len(json_data[name]["BWH"])):
print("{}".format(json_data[name]["BWH"][i]),end="\t")
print()
if __name__=='__main__':
main()
Output result
honoka Height: 157cm BWH: 78 58 82
eri Height: 162cm BWH: 88 60 84
kotori Height: 159cm BWH: 80 58 80
umi Height: 159cm BWH: 76 58 80
rin Height: 155cm BWH: 75 59 80
maki Height: 161cm BWH: 78 56 83
nozomi Height: 159cm BWH: 90 60 82
hanayo Height: 156cm BWH: 82 60 83
niko Height: 154cm BWH: 74 57 79
By the way, when reading the file, I read each element and formatted the display by myself. However, when you want to check the contents of the read JSON file quickly, etc. It is troublesome to display as above (although it is necessary for processing).
For such a case, there is a json.dumps function that formats and displays it.
import json
def main():
f = open("myu_s.json", 'r')
json_data = json.load(f)
#Here is important! !!
#Display with indentation
print("{}".format(json.dumps(json_data,indent=4)))
if __name__=='__main__':
main()
Output result It will be displayed separated by the space specified by the indent of the dumps function. Note that if you do not specify any indent, it will be displayed on one line. Also, since it is read in the normal dictionary format, the order changes.
{
"umi": {
"height": 159,
"BWH": [
76,
58,
80
]
},
"hanayo": {
"height": 156,
"BWH": [
82,
60,
83
]
},
"honoka": {
"height": 157,
"BWH": [
78,
58,
82
]
},
"rin": {
"height": 155,
"BWH": [
75,
59,
80
]
},
"maki": {
"height": 161,
"BWH": [
78,
56,
83
]
},
"nozomi": {
"height": 159,
"BWH": [
90,
60,
82
]
},
"niko": {
"height": 154,
"BWH": [
74,
57,
79
]
},
"eri": {
"height": 162,
"BWH": [{
"umi": {
"height": 159,
"BWH": [
76,
58,
80
]
},
"hanayo": {
"height": 156,
"BWH": [
82,
60,
83
]
},
"honoka": {
"height": 157,
"BWH": [
78,
58,
82
]
},
"rin": {
"height": 155,
"BWH": [
75,
59,
80
]
},
"maki": {
"height": 161,
"BWH": [
78,
56,
83
]
},
"nozomi": {
"height": 159,
"BWH": [
90,
60,
82
]
},
"niko": {
"height": 154,
"BWH": [
74,
57,
79
]
},
"eri": {
"height": 162,
"BWH": [
88,
60,
84
]
},
"kotori": {
"height": 159,
"BWH": [
80,
58,
80
]
}
}
88,
60,
84
]
},
"kotori": {
"height": 159,
"BWH": [
80,
58,
80
]
}
}
The last is about writing files. When writing to a file, if the file is read from another program, there is a possibility that the order will be changed, so a dictionary with the order specified by collections.OrderedDict is created and written.
This time, for the sake of clarity, the data divided into three lists, the name list, the height list, and the three-size list, is written in one JSON structure.
When writing to a file, use the json.dump function. When displayed, dump "s" is. Please note that it is different. Put the dictionary to write in the first argument, and specify the file to write in the second.
import json
import collections as cl
def main():
name_list = ["honoka", "eri", "kotori", "umi", "rin", "maki", "nozomi", "hanayo", "niko"]
height = [157,162,159,159,155,161,159,156,154]
BWH = [[78, 58, 82],[88, 60, 84],[80, 58, 80],[76, 58, 80],
[75, 59, 80],[78, 56, 83],[90, 60, 82],[82, 60, 83],[74, 57, 79]]
ys = cl.OrderedDict()
for i in range(len(name_list)):
data = cl.OrderedDict()
data["BWH"] = BWH[i]
data["height"] = height[i]
ys[name_list[i]] = data
#print("{}".format(json.dumps(ys,indent=4)))
fw = open('myu_s.json','w')
#Here is important! !!
# json.Write to a file with the dump function
json.dump(ys,fw,indent=4)
if __name__=='__main__':
main()
Output result
The above is how to handle JSON files. Please note that there may be some mistakes because it is almost self-taught. If there are any mistakes or new discoveries, we will correct and add them one by one.
[1] Introduction to Python-Lists, Tuples, Dictionaries [2] [python] Format the JSON file and dump it [3] [Introduction to Python] How to handle JSON format data [4] Save the order of adding elements in the python dictionary
Recommended Posts