0
time = [0 1 2 3....]
result = ['normal','limp'....]
def create_dict(a_number):
    return {'Time': time[a_number],
            'Reusult of this person': result[a_number]}
n = 0
final_dict = []
while n < len(time):
    dict = create_dict(n)
    final_dict.append(dict)
    n = n + 1
json_file = json.dumps(final_dict)
with open("sample.json", "w") as outfile:
    outfile.write(json_file)

I created n dictionary within a while loop then append them together, after generating json file, the dict in this file look as one long row,([{"Time": "0 seconds to 2", "Reusult of this person": "limp"}, {"Time": "1 seconds to 3", "Reusult of this person": "normal"}, {"Time": "2 secon) how to change it vertically? origin json file

vertical json file

Andrea0v0
  • 3
  • 3
  • What do you mean by "change it vertically"? – rdas Mar 21 '20 at 06:02
  • use `json.dumps(final_dict, indent=2)` or some number. Note, you can skip `json_file = json.dumps(final_dict)` altogether and just do `with open("sample.json") as outfile: json.dump(final_dict, outfile, indent=2)` i.e., you don't need an intermediate string object, so don't use `json.dumps`, dump to a file object directly using `json.dump` – juanpa.arrivillaga Mar 21 '20 at 06:07

0 Answers0