Facebook
From NovaUser, 4 Years ago, written in Python.
Embed
Download Paste or View Raw
Hits: 20
  1. import re
  2.  
  3. file_in = input() # входные данные
  4. if not file_in: # проверка имени входного файла
  5.     file_in = "JSON_original.txt"
  6. file_out = input() # имя файла выходного
  7. if not file_out: # проверка имени выходного файла
  8.     file_out = file_in + ".YAML.txt"
  9. with open(file_in, "r", encoding='utf-8') as json_in, \
  10.     open(file_out, "w", encoding='utf-8') as yaml_out:
  11.     yaml_out.write("---\n")
  12.     idict = []
  13.     k = 0
  14.     for line in json_in:
  15.         line = line.strip()
  16.         ss = 0
  17.         mm = 0
  18.         if line.find("{") > -1:
  19.             ss = 1
  20.         line = line.replace("{", "")
  21.         if line.rfind("}") > -1:
  22.             ss = -1
  23.         line = line.replace("}", "")
  24.         if line.find("[") > -1:
  25.             mm = 1
  26.         line = line.replace("[", "")
  27.         if line.rfind("]") > -1:
  28.             mm = -1
  29.         line = line.replace("]", "")
  30.         if line[-1:] == ',':
  31.             line = line[:-1]
  32.         if line:
  33.             if re.search(":", line):
  34.                 ltmp = line.split(":")
  35.                 ltmp[0] = ltmp[0].replace("\"", "")
  36.                 line = ":".join(ltmp)
  37.             width = len(idict) - 1
  38.             if len(idict) > 1 and idict[-2][-1] == 1:
  39.                 if idict[-1][-1] == 0:
  40.                     idict[-1][-1] = 1
  41.                     line = "- " + line
  42.                     width -= 1
  43.                 width -= k
  44.             yaml_out.write("  " * width + line + "\n")
  45.         if mm == 1:
  46.             idict[-1].append(mm)
  47.             k += 1
  48.         elif mm == -1:
  49.             del idict[-1][-1]
  50.             k -= 1
  51.         if ss == 1:
  52.             idict.append([mm])
  53.         elif ss == -1:
  54.             del idict[-1]

Replies to Untitled rss

Title Name Language When
Re: Untitled NovaUser python 4 Years ago.
captcha