Python - DIY - Use dump to take some key-value pairs from json to synthesize a new json file

c++

Foreword:

Vue框架:Learn Vue
OJ算法系列:magic tricks from the project- Detailed algorithm explanation
Linux操作系统:of Fenghouqimen-linux
C++11:Tongtianlu-C++11

Application scenario:

Read a json file and use some key-value pairs to build a new json file
  • For example, get the key-value pairs of certain lines in the following json file, and then build a json file
{
    
    
	"packs": [{
    
    
		"_id": "1",
		"time": 123,
		"category": 0,
		"current_info": {
    
    
			"tag": ["timestamp"],
			"fps": 60.0,
			"time_stamp": 414.514,
			"name": "test"
		},
		"content": {
    
    
			"core": "service",
			"status": 0,
			"extraction": "client"
		}
	}]
}

Basic tools:

File operations:

open a file:

  • open('file path', 'open method'):
    1. w: Overwrite
    2. r: read only
    3. wb: binary write
    4. rb: binary read

write file:

  • write(string):
    escape characters in the string:
    1. \r: switch to the first column of the current line
    2. \n: switch to the same column of the next line
file = open('Json文件地址', 'w')
lines = ''
for line in file:
	#line自动省略\r\n
    lines += line

read file:

  • open() returns an iterable object:
file = open('Json文件地址', 'r')
lines = ''
for line in file:
	#line自动省略\r\n
    lines += line

Close the file and flush the buffer:

  • close:
file.close()
  • The buffer is automatically flushed after close(), but sometimes you need to flush it before closing, then you can use the flush() method:
file.flush()

Json string and dictionary conversion:

json.loads():

  • for loading strings as dictionaries
file = open('Json文件地址', 'r')
lines = ''
for line in file:
    lines += line
dic = json.loads(lines)
print(dic)

json.dumps():

  • for writing a dictionary to a string
dic = {
    
    'packs': [{
    
    '_id': '1', 'time': 123, 'category': 0, 'current_info': {
    
    'tag': ['timestamp'], 'fps': 60.0, 'time_stamp': 414.514, 'name': 'test'}, 'content': {
    
    'core': 'service', 'status': 0, 'extraction': 'client'}}]}

json_str = json.dumps(dic)

print(json_str)

Json file and dictionary conversion:

json.load():

  • for loading files as dictionaries
dictionary = json.load(open('原本的Json文件地址', 'r'))
print(dictionary)

json.dump():

  • It is used to write the dictionary to the file, and the created file is opened in w mode
json.dump(ret, open("新的Json文件地址","w"))

Integrate the above API as a function:

def func(dictionary,*direct_key, **indirect_key):
    ret = {
    
    }
    for i in direct_key:
        ret[i] = dictionary[i]
    for i in indirect_key:
        ret[i] = {
    
    }
        print(type(ret), type(ret[i]), i)
        for j in indirect_key[i]:
            print(j)
            ret[i][j] = dictionary[i][j]
    return ret

ret = func(json.load('原来的Json文件地址'), '_id', 'end_time', 'frames', ext=['core_cm', 'extraction_cm'], statistics=['parsing_begin', 'parsing_end'])

json.dump(ret, open("新的Json文件地址","w"))

Develop a json.dump yourself:

  • Simplified version that can only accept up to two pairs of json dictionaries, and the final value is str, int, or float
import os
import json
def isIntOrFloatSeriously(number):
    result = False
    try:
        n = float(number)
        if str(number).count('.') != 0 or (n.is_integer() and str(number).count('.') == 0):
            result = True
    except:
        result = False
    return result

def func(dictionary,*direct_key, **indirect_key):
    amount = len(direct_key) + len(indirect_key)
    count = 1
    ret = ''
    for i in direct_key:
        ret += f"\"{
      
      i}\": "
        if isIntOrFloatSeriously(dictionary[i]) == True :
            ret += str(dictionary[i])
        else:
            ret += "\""+dictionary[i]+"\""
        if count < amount:
            ret += ","
        ret += "\r\n"
        count = count + 1
    for i in indirect_key:
        ret += f"\"{
      
      i}\": "+'{\r\n'
        inner_count = 1
        inner_amount = len(indirect_key[i])
        print(inner_amount, indirect_key[i])
        for j in indirect_key[i]:
            ret += f"\"{
      
      j}\": "
            if isIntOrFloatSeriously(dictionary[i][j]) == True :
                ret += str(dictionary[i][j])
            else:
                ret += "\""+dictionary[i][j]+"\""
            if inner_count < inner_amount:
                ret += ","
            ret += "\r\n"
            inner_count = inner_count + 1
        ret += "}"
        if count < amount:
            ret += ","
        ret += "\r\n"
        count = count + 1
    return ret

file = open('原本的Json文件地址')
lines = ''
for line in file:
    lines += line
dic = json.loads(lines)

ret = func(dic["packs"][0], '_id', 'end_time', 'frames', ext=['core_cm', 'extraction_cm'], statistics=['parsing_begin', 'parsing_end'])

file = open('新建的Json文件地址', 'w')
file.write('{\r\n')
file.write(ret)
file.write('}\r\n')
file.close()

extension:

  • Yaml file format
  • Convert yaml files to dictionaries
    1. yaml.safe_load(): do not omit the .0 of float
    2. yaml.load(): omit .0 of float

Guess you like

Origin blog.csdn.net/buptsd/article/details/129251036