简体   繁体   中英

Sort with spaces a JSON File from a dataframe in Pandas

I'm exporting a dataframe to a JSON file, through these lines of code:

with open('example.json', 'w') as f:
for row in df3.iterrows():
    row[1].to_json(f, orient=None, lines=False)
    f.write("\n") 

And it returns a file like this:

{"age":20,"city":"Burdinne","email":"enim@risus.org","name":"Zorita","phone":4565434645.0,"postal_code":42680.0,"regDate":"2015-06-14T12:12:00-07:00"}
{"age":22,"city":"Bharatpur","email":"purus.mauris.a@odiosagittis.ca","name":"Mariam","phone":null,"postal_code":null,"regDate":"2016-10-14T18:52:48-07:00"}
{"age":28,"city":"Neerheylissem","email":"Nam@enimEtiam.org","name":"Malik","phone":null,"postal_code":null,"regDate":"2016-09-20T18:06:55-07:00"}
{"age":24,"city":"San Fratello","email":"sapien@Nullamlobortis.ca","name":"Claire","phone":null,"postal_code":null,"regDate":"2016-12-29T09:49:13-08:00"}
{"age":30,"city":"La Cruz","email":"tempor@purusmaurisa.edu","name":"Hilel","phone":null,"postal_code":null,"regDate":"2016-07-09T12:03:31-07:00"}

However, I would like that JSON file to be tabulated like this:

[
  {
    "name": "Zorita",
    "email": "enim@risus.org",
    "regDate": "2015-06-14T12:12:00-07:00",
    "city": "Burdinne",
    "age": 20,
    "postal_code":42680,
    "phone": 4565434645
  },
  {
    "name": "Mariam",
    "email": "purus.mauris.a@odiosagittis.ca",
    "regDate": "2016-10-14T18:52:48-07:00",
    "city": "Bharatpur",
    "age": 22
  },
  {
    "name": "Malik",
    "email": "Nam@enimEtiam.org",
    "regDate": "2016-09-20T18:06:55-07:00",
    "city": "Neerheylissem",
    "age": 28
  },
  {
    "name": "Claire",
    "email": "sapien@Nullamlobortis.ca",
    "regDate": "2016-12-29T09:49:13-08:00",
    "city": "San Fratello",
    "age": 24
  },
  {
    "name": "Hilel",
    "email": "tempor@purusmaurisa.edu",
    "regDate": "2016-07-09T12:03:31-07:00",
    "city": "La Cruz",
    "age": 30
  }
]

How could I do this? In my code I'm trying to put the line break with "\\ n" but apparently I'm not doing it correctly

Try below code:

final_list = list()
for row in df3.iterrows():
    final_list.append(row[1].to_dict(orient=None))

with open('example.json', 'w') as f:
    f.write(json.dumps(final_list, indent=4))

You can convert column to list and write to file by json.dump with parameter indent and if necessary sort_keys=True for pretty json:

import json

with open("example.json", "w") as f:
    json.dump(df[1].tolist(), f, indent=4, sort_keys=True)

Sample :

d = [
  {
    "name": "Zorita",
    "email": "enim@risus.org",
    "regDate": "2015-06-14T12:12:00-07:00",
    "city": "Burdinne",
    "age": 20,
    "postal_code":42680,
    "phone": 4565434645
  },
  {
    "name": "Mariam",
    "email": "purus.mauris.a@odiosagittis.ca",
    "regDate": "2016-10-14T18:52:48-07:00",
    "city": "Bharatpur",
    "age": 22
  }

]

df = pd.DataFrame({1: d})
#print (df)

import json

with open("example.json", "w") as f:
    json.dump(df[1].tolist(), f, indent=4, sort_keys=True)

[
    {
        "age": 20,
        "city": "Burdinne",
        "email": "enim@risus.org",
        "name": "Zorita",
        "phone": 4565434645,
        "postal_code": 42680,
        "regDate": "2015-06-14T12:12:00-07:00"
    },
    {
        "age": 22,
        "city": "Bharatpur",
        "email": "purus.mauris.a@odiosagittis.ca",
        "name": "Mariam",
        "regDate": "2016-10-14T18:52:48-07:00"
    }
]

Although this has been answered by @skaul05 , using iterrows can be inefficient. This might be better

with open('file.json', 'w') as f:
    f.write(json.dumps(json.loads(df.to_json()), indent=4))

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