下载json文件

点击这里进入下载页面,右键从链接另存文件

获得的json文件如图:

python中json的基本操作

JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式,在python中可使用json包进行解码与编码,主要涉及四个方法:load, loads, dump, dumps

  • json.loads:将json字符串解码为python对象
  • json.dumps:将python对象编码为json字符串
  • json.loadjson.dump:分别与json.loads, json.dumps的功能对应,但需要传入文件描述符,对文件进行操作
1
2
3
4
5
6
7
import json

with open('test.json', 'r', encoding='utf-8') as f:
dict = json.load(f)
# 等同于:
with open('test.json', 'r', encoding='utf-8') as f:
dict = json.loads(f.read())

解析ko00001.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
import json
import re

class KOParser(object):
def __init__(self, map):
self.map = map

def json_parser(self):
# check the existance of json file
if os.path.exists(self.map):
with open(self.map, 'r') as f:
ko_list = []
# convert json to dict
map_dict = json.load(f)
# print(type(map_dict)) dict
maps = map_dict['children']
# print(type(maps)) list
for map in maps:
map_name = map['name'][0:5] + '\t' + map['name'][6:]
# print(map_name)
# print(type(map['children']))
for map_l_1 in map['children']:
map_l_1_name = map_l_1['name'][0:5] + '\t' + map_l_1['name'][6:]
# print(map_l_1_name)
for pathway in map_l_1['children']:
try:
for genes in pathway['children']:
pathway_name = pathway['name'][0:5] + '\t' + pathway['name'][6:]
# print(genes['name'])
k_num = genes['name'].split(sep=' ')[0]
gene_name = genes['name'].split(sep=' ')[1].split(sep=';')[0]
anno = genes['name'].split(sep=' ')[1].split(sep=';')[-1]
try:
pattern = re.compile('(.*)(\[EC:.*\])')
product = re.search(pattern, anno).group(1)
ec = re.search(pattern, anno).group(2)
ko = k_num + '\t' + gene_name + '\t' + product + '\t' + ec
except:
ko = k_num + '\t' + gene_name + '\t' + anno
# print(ko)
info = map_name + '\t' + map_l_1_name + '\t' + pathway_name + '\t' + ko
ko_list.append(info)
except:
continue
return ko_list
else:
print('Error: Json file does not exist.')

def save_data(self, result, file_name):
# convert list to string for writing as file
for info in result:
kos = ''.join(info) + '\n'
with open(file_name, 'a', encoding='utf-8') as f:
f.write(kos)


if __name__ == '__main__':
map = 'ko00001.json'
file_name = 'ko.txt'
ko_json = KOParser(map)
ko = ko_json.json_parser()
ko_json.save_data(ko, file_name)