0

I want to know if the json of firebase real database are jsons because there is an id in front of each object. Always i had the id as a one attribute more of the object, but no like this, and i don't know if i can't change or how to manage jsons like this. I want in python to convert this in csv and with this format i dont know how. I also want to create a string array with all the field pregutas but i dont know how i can do it because there is an id . Can anybody help me ?? maybe i can change the config in firebase i dont know

{

      "-MNix6aRlRNhKx7BkWxN": {
        "pregunta": "Hago los deberes", 
        "respuesta": 1
      }, 
      "-MNixa7sMvm-Q_vg33Bm": {
        "pregunta": "Salto desde un puente ?", 
        "respuesta": 0
      }, 
      "-MNixiWz0ChWsPbs0KS5": {
        "pregunta": "Le pido de salir a mi crush ?", 
        "respuesta": 1
      }, 
      "-MNixnIqTeDU36slCVcX": {
        "pregunta": "Me fumo un piti", 
        "respuesta": 0
      }, 
      "-MNixszgq98myndY0lfw": {
        "pregunta": "Me ducho ahora mismo ?", 
        "respuesta": 1
      }, 
      "-MO13tm0dpmtogdw6ZCQ": {
        "pregunta": "Voy a ser papa?", 
        "respuesta": 0
      }, 
      "-MO1Q7NVvqO76cgjfJAV": {
        "pregunta": "Esto esta avanzando aunque sea lento?", 
        "respuesta": 0
      }, 
      "-MO1QCvTWUk6LlztWY91": {
        "pregunta": "Voy a morir ma\u00f1ana", 
        "respuesta": 0
      }
    }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Joan Codinach Ortiz
  • 381
  • 1
  • 2
  • 10

1 Answers1

1

Assuming that you have the data structure shown in your question in a variable named response, then the code:

import csv, sys
writer = csv.writer(sys.stdout)
writer.writerows([(r["pregunta"], str(r["respuesta"])) for r in response.values()])

...will write only the pregunta and respuesta fields to a CSV on stdout. Thus, for your sample input, output will be:

Hago los deberes,1
Me ducho ahora mismo ?,1
Me fumo un piti,0
Voy a morir ma\u00f1ana,0
Le pido de salir a mi crush ?,1
Esto esta avanzando aunque sea lento?,0
Salto desde un puente ?,0
Voy a ser papa?,0
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • okey, thank you very much, and if i want to create the same but with keys ? , the first column pregunta and¡ the second respuesta ? because i do that and dont know which column is review = re.sub('[^a-zA-Z]', ' ', dataset['pregunta'][i]) – Joan Codinach Ortiz Dec 09 '20 at 11:13
  • The CSV created by the code above _already does_ have the first column `pregunta` and the second `respuesta`. I'm not clear what it is you're asking for that's different. – Charles Duffy Dec 09 '20 at 12:16
  • ...if you want to filter out non-printable characters, how to do that is a separate question -- and it's one that's already asked-and-answered, f/e at [Stripping non-printable characters from a string in Python](https://stackoverflow.com/questions/92438/stripping-non-printable-characters-from-a-string-in-python). How the `[^A-Za-z]` approach works is very locale-dependent; I don't recommend it. – Charles Duffy Dec 09 '20 at 12:19
  • what happens is that now with this csv the " " disappear and then when i try to read_csv it makes this error 'utf-8' codec can't decode byte 0xf1 in position 14: invalid continuation byte , it has to be "Hago los deberes" , "1" . How i can do it ?? – Joan Codinach Ortiz Dec 09 '20 at 14:52
  • No, you don't need to delete that character, you just need to encode it as UTF-8 (since that's what your code doing the reading on the other end expects). – Charles Duffy Dec 09 '20 at 15:01
  • and how i can encode it as UTF-8 ?? – Joan Codinach Ortiz Dec 09 '20 at 16:23
  • First thing we need to know to do that correctly: what's the source encoding? – Charles Duffy Dec 09 '20 at 18:55