forked from Boris-code/feapder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_json.py
More file actions
52 lines (41 loc) · 1.22 KB
/
create_json.py
File metadata and controls
52 lines (41 loc) · 1.22 KB
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
# -*- coding: utf-8 -*-
"""
Created on 2018-08-28 17:38:43
---------
@summary: 字符串转json
---------
@author: Boris
@email: boris_liu@foxmail.com
"""
import sys
import feapder.utils.tools as tools
class CreateJson:
def get_data(self):
"""
@summary: 从控制台读取多行
---------
---------
@result:
"""
print("请输入需要转换的内容: (xxx:xxx格式,支持多行)")
data = []
while True:
line = sys.stdin.readline().strip().replace("\t", " " * 4)
if not line:
break
data.append(line)
return data
def create(self, sort_keys=False):
contents = self.get_data()
json = {}
for content in contents:
content = content.strip()
if not content or content.startswith(":"):
continue
regex = "([^:\s]*)[:|\s]*(.*)"
result = tools.get_info(content, regex, fetch_one=True)
if result[0] in json:
json[result[0]] = json[result[0]] + "&" + result[1]
else:
json[result[0]] = result[1].strip()
print(tools.dumps_json(json, sort_keys=sort_keys))