U
U
user7770002020-05-23 20:58:11
Python
user777000, 2020-05-23 20:58:11

Working with json files?

def g():
    with open('days.txt', 'r') as f:
        return json.loads(f.read())
j = g()

with open('days_file.txt', 'w') as fileQ:
    json.dump(j, fileQ,  indent=4, ensure_ascii=False)


Imported like this:
"{\n    \"amount\": 2,\n    \"hash\": \"6d0d56cbb80fe275ebfdf8e84ee475a5\",\n    \"name\": \"ivan\",\n    \"to_whom\": \"katja\",\n    \"uuid\": \"000\"\n}"


And I need it like this (i.e. in its original form):
{
    "amount": 2,
    "hash": "6d0d56cbb80fe275ebfdf8e84ee475a5",
    "name": "ivan",
    "to_whom": "katja",
    "uuid": "000"
}


So is it really possible to do it?

Ps json.loads() cannot be removed

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Karbivnichy, 2020-05-23
@user777000

import json

with open('days_file.txt') as file:
  j_data = json.loads(file.read())

json_formatted = json.dumps(j_data, indent=2)
print(json_formatted)

5ec967d64518b121319143.png
It is not clear from the question - do you need to save it in a "beautiful" form or download it?

S
Sergey Gornostaev, 2020-05-23
@sergey-gornostaev

Yes, working with JSON files.

D
Dr. Bacon, 2020-05-23
@bacon

It’s not clear what the problem is, it’s just different formatting, it doesn’t affect the data in any way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question