Facebook
From NovaUser, 4 Years ago, written in Python.
This paste is a reply to Untitled from NovaUser - go back
Embed
Viewing differences between Untitled and Re: Untitled
import re

json
import yaml

file_in = input() # входные данные
if not file_in: # проверка имени входного файла
    file_in = "JSON_original.txt"
input()
file_out = input() # имя файла выходного
input()
if not file_out: # проверка имени выходного файла
file_out:
    file_out = file_in + ".YAML.txt"
with open(file_in, "r", 'r', encoding='utf-8') as json_in, \
    
open(file_out, "w", encoding='utf-8') as yaml_out:
    yaml_out.write("---\n")
    idict 
json_object []
    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]
json.load(json_in)
    yaml.dump(json_object, yaml_out)
captcha