giovedì 1 aprile 2021

PYTHON - JSON MAKER

Maker JSON Files
Writing a json file can be boring, with this very simple script it's boring all the same, but at least in the end everything is correct.
########################################
# json files editor to use as database #
########################################

import sys,os
import curses
import json

def interpreta(o,file,next,cont):
    if next=='#k' and cont==0:
        file=file+'\"'+o+'\":'
        next=''
        cont+=1
    elif next=='#k' and cont>0:
        file=file+',\"'+o+'\":'
        next=''
    elif next=='#v':
        file=file+'\"'+o+'\"'
        next=''
    elif next=='#vl':
        c=o.split(',')
        file=file+'['
        for i in range(len(c)):
            file=file+'\"'+c[i]+'\"'
            if i!=len(c)-1:
                file=file+','
        file=file+']'
        next=''
    if o=='#k' and cont==0:
        file='{'
        next='#k'
    if o=='#k' and cont>0:
        next='#k'
    if o=='#v':
        next='#v'
    elif o=='#vl':
        next='#vl'
    elif o=='#vd':
        next='#vd'
    return file,next,cont

def main():
    print("JSON maker")
    print("commands: #k new key - #v new value - #vl new list value - #e save and exit")
    file=''
    cont=0
    next=''
    nomeFile=''
    while True:
        o=input(">")
        if o=='#e':
            file=file+"}"
            nomeFile=input("Nome del file >")
            f=open(nomeFile,"w")
            f.write(file)
            f.close()
            sys.exit()
        file,next,cont=interpreta(o,file,next,cont)


if __name__ == "__main__":
    main()

Nessun commento:

Posta un commento