1

I have a json file that is a synonime dicitonnary in French (I say French because I had an error message with ascii encoding... due to the accents 'é',etc). I want to read this file with python to get a synonime when I input a word. Well, I can't even read my file... That's my code:

data=[]
with open('sortieDES.json', encoding='utf-8') as data_file:
    data = json.loads(data_file.read())
print(data)

So I have a list quite ugly, but my question is: how can I use the file like a dictionary ? I want to input data['Académie']and have the list of the synonime... Here an example of the json file:

{"Académie française":{
"synonymes":["Institut","Quai Conti","les Quarante"]
}
0

2 Answers 2

2

You only need to call json.load on the File object (you gave it the name data_file):

data=[]
with open('sortieDES.json', encoding='utf-8') as data_file:
    data = json.load(data_file)
print(data)
Sign up to request clarification or add additional context in comments.

3 Comments

When i do this i have this error: 'the JSON object must be str, not 'TextIOWrapper''. I edited my post because my problem is different now :(
@pioupiou1211 are you using json.load or json.loads? json.load takes a file object, which is what I demostrated above. json.loads takes a string.
oh yeah my bad, seems that you resolved my problem! I have a nice dictionnary and i can do everything i want with it :) THanks a lot!
-1

Instead of

json.load(line)

you have to use

json.loads(line)

Your s is missing in loads(...)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.