简体   繁体   中英

Custom file mapping python json to custom

I have recently created a parser in python 3 that converts files from a custom format (let's call the format .querty) into JSON (.json) for easier manipulation of its contents. Now I'm wondering what is the best way to translate back the JSON into my original format keeping all its original structure.

An example of the files can be seen below,

example.qwerty

Dict_abc_1{

    Dict_abc_2{
        HeaderGUID="";
        Version_TPI="999";
        EncryptionType="0";
    }

    Dict_abc_3{
        FamilyName="John Doe";
    }

    Dict_abc_4{
        Array_abc{
            {TimeStamp="2018-11-07 01:00:00"; otherinfo="";}
            {TimeStamp="2018-11-07 01:00:00"; otherinfo="";}
            {TimeStamp="2018-11-07 01:00:00"; otherinfo="";}
            {TimeStamp="2018-11-07 02:53:57"; otherinfo="";}
            {TimeStamp="2018-11-07 02:53:57"; otherinfo="";}
        }

        Dict_abc_5{
            LastContact="2018-11-08 01:00:00";
            BatteryStatus=99;
            BUStatus=PowerOn;
            LastCallTime="2018-11-08 01:12:46";
            LastSuccessPoll="2018-11-08 01:12:46";
            CallResult=Successful;
        }
    }
}
Code=999999;

example.json

{  
    "Dict_abc_1":{
        "Dict_abc_2":{
            "HeaderGUID":"",
            "Version_TPI":"999",
            "EncryptionType":"0"
        },

        "Dict_abc_3":{
            "FamilyName":"John Doe"
        },

        "Dict_abc_4":{
            "Array_abc":[
                {"TimeStamp":"2018-11-07 01:00:00", "otherinfo":""},
                {"TimeStamp":"2018-11-07 01:00:00", "otherinfo":""},
                {"TimeStamp":"2018-11-07 01:00:00", "otherinfo":""},
                {"TimeStamp":"2018-11-07 02:53:57", "otherinfo":""},
                {"TimeStamp":"2018-11-07 02:53:57", "otherinfo":""}
            ],

            "Dict_abc_5":{
                "LastContact":"2018-11-08 01:00:00",
                "BatteryStatus":99,
                "BUStatus":"PowerOn",
                "LastCallTime":"2018-11-08 01:12:46",
                "LastSuccessPoll":"2018-11-08 01:12:46",
                "CallResult":"Successful"
            }
        }
    },
    "Code":999999
}

Structural definitions of .qwerty that are different to json

  • Dictionaries/Arrays don't have separates between them and their key
  • Dictionaries/Arrays have two line breaks after the definition
  • Variables use '=' as a separator between them and their value
  • Variables use ';' after their value
  • Arrays use {} instead of []

As my current .qwerty to .json parser uses lexical and syntactical analysis. I don't think it is necessary to recreate another parser using this method as the data is in a very manipulable form now (json). I was wondering if it would be a good idea to extend the json.dumps to adapt for my new definitions but don't know where to start or if it is possible.

I'm trying to do this an efficient manner, thoughts or approaches are much appreciated, Thank you.

Since your syntax looks relatively simple, you may be able to get away with not using lexical and syntactical analysis. Instead, you could use some regular expressions to recognize your arrays/dictionaries and variables, and just modify the formatting accordingly using simple replacement.

I think this would work in a brute-forcey sort of way, though I haven't done any testing on it. One notable point of difficulty would be with lists, as this code snippet will not work if lists can contain anything besides dicts. You might have to adapt that part of the code - or if the contents of the dict inside the list doesn't have to be all on one line, you could make that run recursively as well. But regardless, hopefully this provides a good start for you to experiment with.

It's a recursive function. Three inputs: the dictionary to be parsed, a file to which to write, and the current level of indentation.

def print_qwerty(json_dict, file_descriptor, indent_level=0):
    for (k, v) in json_dict.items():
        if type(v) == dict:
            file_descriptor.write('    '*indent_level + k + '{\n')
            print_qwerty(v, file_descriptor, indent_level + 1)
            file_descriptor.write('    '*indent_level + ';}\n')
        elif type(v) == list:
            for i in v:
                args_str = ['{}="{}"'.format(k2,v2) for (k2, v2) in i.items()]
                file_descriptor.write('    '*indent_level + '{' + '; '.join(args_str) + '}\n')
        else:
             file_descriptor.write('    '*indent_level + '{}="{}";\n'.format(k, v))

Example usage:

import json
with open('example.json', 'r') as json_file:
    json_dict = json.loads(json_file.read())
with open('example.qwerty', 'w') as 'qwerty_file':
    print_qwerty(json_dict, qwerty_file)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM