I want to write to a file without replacing the text with a "dict" without the "" of the string

1
mind = {"message": "null", "mID": "0", "reply": "null"}    

mind["mID"] = random.randint(0,1000000000000)
mind["message"] = getMessage
mind["reply"] = strReply 

Assuming a hypothetical operation only, you would want to get that by calling the function (let's say write) mindTEXT = temp.write(mind) write in TEMP.json

{"mensaje": "hola", "miID": "123", "reply": "hola"}

(The content of the dict can be ignored, I just want to know that at the time of writing it is changed)

I want to achieve it "clean" to say it in some way. But when calling the function write (transforming mind into a String previously) as it is logical it returns the message as a string: "{"mensaje": "hola", "miID": "123", "reply": "hola"}"

What I want to achieve is to remove those "" so that I can use it as a dict for python using the library JSON .

I forgot to mention the functionality, maybe it helps:

The goal is that when I write in X input that data is stored in a JSON but does not overwrite the current file (so that dump does not work for me). My goal would be something like this:

z = input("Escribe :")

Now I pass z for a function which I would pass to achieve 2 data:

  • getMessage (The message I've written)

  • strReply (Still does not have functionality, but returns "null")

With this data I want to write them in a JSON in this way (from dict to JSON):

MIND.json
---------

{"mensaje": "hola", "miID": "123", "reply": "hola"}

This way I could use it from python as if it were a dict.

    
asked by guskikalola 18.11.2018 в 00:49
source

1 answer

1

If I did not get it wrong, I think what you want to do is add a new line with another JSON object to an existing file.

I mean, I guess you have something like this:

datos = funcion_que_genera_diccionario_python()
# Ahora datos es un diccionario python que contiene por ejemplo
# {"mensaje": "hola", "miID": "123", "reply": "hola"}

# Abrimos fichero en modo "a" para añadir
with open("fichero.json", "a") as f:
   # Convertimos en cadena el diccionario python
   cadena = json.dumps(datos)
   # Y la añadimos al fichero
   f.write(cadena)
   # Retorno de carro
   f.write("\n")

If you run that several times, the fichero.json would have something like this:

{"mensaje": "hola", "miID": "123", "reply": "hola"}
{"mensaje": "hola", "miID": "123", "reply": "hola"}
{"mensaje": "hola", "miID": "123", "reply": "hola"}

If that is your goal, the previous example should do what you expect. As you will see in the json file, the quotes around each line are not saved as you mentioned in the question.

However I must warn you that this file is not a valid JSON, it contains several objects and a valid JSON must contain only one (although that one could be a list in which there is more).

That is, if you try to read the previous file as if it were a JSON like this:

with open("fichero.json") as f:
    datos = json.load(f)

You will get the error

json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 53)

because once he has read the first object he does not wait anymore, but finds more.

If you want to be able to re-read as JSON what you have uploaded, what you have to do is that that file save a list instead of a series of dictionaries. Something like this:

datos = funcion_que_genera_diccionario_python()

# Leemos del fichero JSON todos los diccionarios que teníamos
# guardados de ejecuciones anteriores, que serán una lista
with open("fichero.json") as f:
   anteriores = json.loads(f)

# Añadimos el nuevo diccionario a la lista anterior
anteriores.append(datos)

# Volvemos a abrir el fichero y volcamos la nueva lista
with open("fichero.json", "w") as f:
    json.dump(anteriores, f)

To work properly, the fichero.json should exist before running the program for the first time, and contain an empty list, like this:

[]

After several executions, the file would contain something like this (I format it so it looks better, but it would probably be all stuck in a single line):

[
{"mensaje": "hola", "miID": "123", "reply": "hola"},
{"mensaje": "hola", "miID": "123", "reply": "hola"},
{"mensaje": "hola", "miID": "123", "reply": "hola"}
]

This is a valid JSON, since it is a single object: a list that has several dictionaries inside.

Note on efficiency

The previous approach guarantees that the fichero.json always has valid JSON, but at the cost of each time the program is executed it takes longer to generate that file (since it must read and process what it had before, add another dictionary) and turn everything over again.)

The first approach that simply added lines to the file when opening it in "a" mode was much more efficient, but it had the disadvantage that what it generated was not valid JSON, so you could not read it with json.load() .

A possible intermediate solution is to use the "append" mode as in the first part of the answer, but when you go to read the file, do not try to do it in a single json.load() , which will fail, but process it line by line and each line (which would be a string with a valid JSON) will parse it with json.loads() .

So for example:

todos_los_json = []
with open("fichero.json") as f:
   for linea in f:
       todos_los_json.append(json.loads(lineea))

At the end of the loop you will have a list in which each element is a python dictionary.

    
answered by 18.11.2018 / 01:34
source