Uncategorized

Remove the \ added by the json module of python when parsing



I want to remove \ appearing before ” when I parse my json file with json.dumps(). I would like to avoid processing the file again through python but if this is not possible otherwise I’ll do with it.

My json should look like this :

{
    "Monster Name": "Dragon"
    "skillList": [
         "Fire breath", "Claw"
    ]
}

But it looks like that :

{
    "Monster Name": "Dragon"
    "skillList": [
         "\"Fire breath\", \"Claw\"
    ]
}

I tried to modify the way I pass the args (js.MonsterAddition(Name,MSet),js.MonsterAddition(Name,[MSet])) and the way I parse this (json.dumps({"Name":Name,"skillList":[skillList]}),json.dumps({"Name":Name,"skillList":skillList})) But for now, all I’ve accomplished was to put a dictionnary in another one or don’t put any dictionary.

I get all the inputs from an entry on a GUI made with PyGObjets.

For now my whole code is in two different files :

import sys
import gi
import jsonlibfunctions as js

gi.require_version("Gtk", "3.0")
from gi.repository import GLib, Gtk

IDName = ""
class SimpleApp(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Modding GUI")
        self.set_default_size(400, 300)
        self.set_border_width(10)

        # Create a vertical box layout
        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
        self.add(vbox)

        # Create a button to exit
        self.buttonexit = Gtk.Button(label="Exit")
        self.buttonexit.connect("clicked", self.on_button_clicked)
        self.buttonexit.set_halign(Gtk.Align.CENTER)
        vbox.pack_start(self.buttonexit, True, True, 0)

        # Create a label
        self.label = Gtk.Label(label="What do you want to create today ?")
        self.label.set_halign(Gtk.Align.CENTER)
        vbox.pack_start(self.label, True, True, 0)

        # Create a button to open a new window
        self.buttonMA = Gtk.Button(label="Create an addition to an already existing monster")
        self.buttonMA.connect("clicked", self.on_MA_clicked)
        self.buttonMA.set_halign(Gtk.Align.CENTER)
        vbox.pack_start(self.buttonMA, True, True, 0)

        # Center the box in the window
        vbox.set_valign(Gtk.Align.CENTER)
        vbox.set_halign(Gtk.Align.CENTER)

    def on_button_clicked(self, widget):
        self.destroy()

    def on_MA_clicked(self, widget):
        MA_window = Gtk.Window(title="MA creator")
        MA_window.set_default_size(300, 200)

        # Create a vertical box layout for the new window
        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
        MA_window.add(vbox)

        # Create a text entry
        entry = Gtk.Entry()
        entry.set_placeholder_text("Enter the Monster name here")
        vbox.pack_start(entry, True, True, 0)

        entryMS = Gtk.Entry()
        entryMS.set_placeholder_text("Enter the Monster Move Set addition here")
        vbox.pack_start(entryMS, True, True, 0)
        # Create a button to parse in the json
        buttonMAgen = Gtk.Button(label="Generate a .json")
        buttonMAgen.connect("clicked", self.generateMA, entry, entryMS)
        vbox.pack_start(buttonMAgen, True, True, 0)

        # Center the entry in the new window
        vbox.set_valign(Gtk.Align.CENTER)
        vbox.set_halign(Gtk.Align.CENTER)

        # Show the new window
        MA_window.show_all()

    def generateMA(self,widget,IDentry,MSentry):
        IDName = IDentry.get_text()
        MSet = MSentry.get_text()
        js.MonsterAddition(Name,MSet)

if __name__ == "__main__":
    win = SimpleApp()
    win.connect("destroy", Gtk.main_quit)
    win.show_all()
    Gtk.main()

And :

import json

def MonsterAddition(Name, skillList):
    print(skillList)
    MA = json.dumps({"IDName":IDName,"skillList":[skillList], indent=4)
    print(MA)
    with open(Name+"Addition.json","w") as outfile:
        outfile.write(MA)



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *