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
56 lines (43 loc) · 1.33 KB
/
Copy pathcreate_json.py
File metadata and controls
56 lines (43 loc) · 1.33 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
53
54
55
56
# -*- coding: utf-8 -*-
"""
Created on 2018-08-28 17:38:43
---------
@summary: 字符串转json
---------
@author: Boris
@email: [email protected]
"""
import pyperclip
import feapder.utils.tools as tools
class CreateJson:
def get_data(self):
"""
@summary: 从控制台读取多行
---------
---------
@result:
"""
input("请复制需要转换的内容(xxx:xxx格式,支持多行),复制后按任意键读取剪切板内容\n")
text = pyperclip.paste()
print(text + "\n")
data = []
for line in text.split("\n"):
line = line.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))