import re
file_in = input() # входные данные
if not file_in: # проверка имени входного файла
file_in = "JSON_original.txt"
file_out = input() # имя файла выходного
if not file_out: # проверка имени выходного файла
file_out = file_in + ".YAML.txt"
with open(file_in, "r", encoding='utf-8') as json_in, \
open(file_out, "w", encoding='utf-8') as yaml_out:
yaml_out.write("---\n")
idict = []
k = 0
for line in json_in:
line = line.strip()
ss = 0
mm = 0
if line.find("{") > -1:
ss = 1
line = line.replace("{", "")
if line.rfind("}") > -1:
ss = -1
line = line.replace("}", "")
if line.find("[") > -1:
mm = 1
line = line.replace("[", "")
if line.rfind("]") > -1:
mm = -1
line = line.replace("]", "")
if line[-1:] == ',':
line = line[:-1]
if line:
if re.search(":", line):
ltmp = line.split(":")
ltmp[0] = ltmp[0].replace("\"", "")
line = ":".join(ltmp)
width = len(idict) - 1
if len(idict) > 1 and idict[-2][-1] == 1:
if idict[-1][-1] == 0:
idict[-1][-1] = 1
line = "- " + line
width -= 1
width -= k
yaml_out.write(" " * width + line + "\n")
if mm == 1:
idict[-1].append(mm)
k += 1
elif mm == -1:
del idict[-1][-1]
k -= 1
if ss == 1:
idict.append([mm])
elif ss == -1:
del idict[-1]
Replies to Untitled 
Title |
Name |
Language |
UNIX |
When |
Re: Untitled |
NovaUser |
python |
1602704100 |
4 Years ago. |
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}