create_json.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on 2018-08-28 17:38:43
  4. ---------
  5. @summary: 字符串转json
  6. ---------
  7. @author: Boris
  8. @email: boris_liu@foxmail.com
  9. """
  10. import sys
  11. import feapder.utils.tools as tools
  12. class CreateJson:
  13. def get_data(self):
  14. """
  15. @summary: 从控制台读取多行
  16. ---------
  17. ---------
  18. @result:
  19. """
  20. print("请输入需要转换的内容: (xxx:xxx格式,支持多行)")
  21. data = []
  22. while True:
  23. line = sys.stdin.readline().strip().replace("\t", " " * 4)
  24. if not line:
  25. break
  26. data.append(line)
  27. return data
  28. def create(self, sort_keys=False):
  29. contents = self.get_data()
  30. json = {}
  31. for content in contents:
  32. content = content.strip()
  33. if not content or content.startswith(":"):
  34. continue
  35. regex = "([^:\s]*)[:|\s]*(.*)"
  36. result = tools.get_info(content, regex, fetch_one=True)
  37. if result[0] in json:
  38. json[result[0]] = json[result[0]] + "&" + result[1]
  39. else:
  40. json[result[0]] = result[1].strip()
  41. print(tools.dumps_json(json, sort_keys=sort_keys))