-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjsonExample.py
39 lines (32 loc) · 1.28 KB
/
jsonExample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import json
### writing JSON:
# simple python dictionary for one sentence:
sentenceObject = {'sentence': 'This is a test sentence', 'level':'A2'}
# to generate the json as a string:
sentenceJsonString = json.dumps(sentenceObject)
# python list with dictionaries inside = our corpus
corpus = [
sentenceObject,
{'sentence': 'Another sentence! nice', 'level':'B1'}
]
# and generate the string again:
corpusJsonString = json.dumps(corpus) # this string now contains our whole corpus in a nice format
# we can now write these strings to a file and voilà, we have a corpus file!
# OR we can load our json strings into Python objects again:
### loading JSON:
# load the json string into a Python object:
pythonObject = json.loads(sentenceJsonString)
# the result is a Python dictionary and can easily be accessed that way:
print("Test sentence:")
print(pythonObject["sentence"] + ", " + pythonObject["level"])
# the same goes for the "corpus":
corpusObject = json.loads(corpusJsonString)
# now our result is a list, so we can iterate over it:
print("\n")
print("Test whole corpus:")
for sentence in corpusObject:
<<<<<<< HEAD
print(sentence["sentence"] + ", " + sentence["level"])
=======
print(sentence["sentence"] + ", " + sentence["level"])
>>>>>>> a0b135bc5b75913a5c0c91420b81aa6c031169b3