V
V
Vladislav Tikhonov2016-05-14 21:58:32
Python
Vladislav Tikhonov, 2016-05-14 21:58:32

How to load data from json into python dictionary?

I decided to make a chat bot in python) I plan to implement it using a dictionary. It is inconvenient to store everything in the code, and very much so in a separate json file. But there is a problem, I don't know how to upload the json file to the dictionary. Write down the answer, preferably in more detail. You need something like this:
Dictionary = {PhrasePerson: Answer}
If you look at the json file, then there is like this:
{
"Hi": "Hi!"
}
And so on.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2016-05-14
@wolterx

Use the json module

import json

#сохранить в json
with open('data.json', 'w', encoding='utf-8') as fh: #открываем файл на запись
    fh.write(json.dumps(data, ensure_ascii=False)) #преобразовываем словарь data в unicode-строку и записываем в файл

#загрузить из json
with open('data.json', 'r', encoding='utf-8') as fh: #открываем файл на чтение
    data = json.load(fh) #загружаем из файла данные в словарь data

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question