Handling of JSON files in Python

1, Python file output

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.

2, About lists and dictionaries

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.

2.1 List

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

2.2 Dictionary

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.

3 JSON format

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

4 samples

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
    }
}

4.1 Read file

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		

4.2 Display in file

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
        ]
    }
}

4.3 Writing a file

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

5 Summary

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.

6 Referenced sites

[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

Handling of JSON files in Python
Read and write JSON files in Python
Sample for handling eml files in Python
Hexadecimal handling in Python 3
Summary of how to import files in Python 3
Reading and writing CSV and JSON files in Python
Batch conversion of Excel files to JSON [Python]
Easily format JSON in Python
Handling of quotes in [bash]
Equivalence of objects in Python
python> Handling of 2D arrays
Handling of python on mac
Implementation of quicksort in Python
Relative url handling in python
Transpose CSV files in Python Part 1
Pixel manipulation of images in Python
Handling of sparse tree-structured attributes (Python)
Write JSON Schema in Python DSL
Division of timedelta in Python 2.7 series
MySQL-automatic escape of parameters in python
Dynamically load json type in python
Download Google Drive files in Python
Implementation of life game in Python
Waveform display of audio in Python
Sort large text files in Python
Handling timezones in Python (datetime, pytz)
Python #JSON
Read files in parallel with Python
Comparison of data frame handling in Python (pandas), R, Pig
Export and output files in Python
Law of large numbers in python
Implementation of original sorting in Python
Reversible scrambling of integers in Python
A set of script files that do wordcloud in Python3
Extract strings from files in Python
Get a list of files in a folder with python without a path
Conversion of string <-> date (date, datetime) in Python
Data input / output in Python (CSV, JSON)
Check the behavior of destructor in Python
(Bad) practice of using this in Python
General Theory of Relativity in Python: Introduction
Find files like find on linux in Python
Display a list of alphabets in Python 3
Comparison of Japanese conversion module in Python3
Summary of various for statements in Python
Create a JSON object mapper in Python
Type annotations for Python2 in stub files!
Referencing INI files in Python or Ruby
The result of installing python in Anaconda
Automate jobs by manipulating files in Python
Reading and writing JSON files with Python
Gang of Four (GoF) Patterns in Python
The basics of running NoxPlayer in Python
Bulk replacement of strings in Python arrays
Project Euler # 16 "Sum of Powers" in Python
Traffic Safety-kun: Recognition of traffic signs in Python
Summary of built-in methods in Python list
Non-logical operator usage of or in python
In search of the fastest FizzBuzz in Python
Download files in any format using Python
Practical example of Hexagonal Architecture in Python