-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_translate.py
72 lines (49 loc) · 2.15 KB
/
json_translate.py
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
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3
# encoding: utf-8
# @author: hoojo
# @email: hoojo_@126.com
# @github: https://github.com/hooj0
# @create date: 2018-06-30 19:48:03
# @copyright by hoojo@2018
# @changelog Added python3 `json translate` example
import json
#===============================================================================
# 示例:gitmojis.json 和 git-emoji-list.rst 进行读取和转换
#===============================================================================
# 描述:将gitmojis.json文件中的 emoji、description 属性,
# 替换成 git-emoji-list.rst 文件中的中文描述。
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# 读取git-emoji-list.rst文件内容
#-------------------------------------------------------------------------------
data = None
with open('gitmojis.json', 'r', encoding=u'utf-8') as f:
data = json.load(f)
print("json 原始数据:", data)
#-------------------------------------------------------------------------------
# 读取gitmojis.json文件内容
#-------------------------------------------------------------------------------
chineses = {}
with open('git-emoji-list.rst', 'r', encoding=u'utf-8') as f:
text = f.readline()
text = f.readline()
while len(text) > 0:
text = f.readline()
items = text.split("|")
if len(items) > 3:
names = items[1].strip().split("(")
chineses[names[0].strip()] = { "emoji": names[1].strip().replace(")", ""), "description": items[3].strip() }
#-------------------------------------------------------------------------------
# 数据替换
#-------------------------------------------------------------------------------
size = 0
for item in data:
record = chineses[item["code"]]
if size < len(record["emoji"]):
size = len(record["emoji"])
item["emoji"] = record["emoji"].ljust(10)
item["code"] = item["code"].ljust(30)
item["description"] = record["description"]
#print(size)
#print(data)
print(json.dumps(data))