Skip to content

Commit f6cbab8

Browse files
committed
add example on reading writing utf-8 using json
1 parent 2362c62 commit f6cbab8

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
Save UTF to JSON file and then read it.
6+
"""
7+
8+
import json
9+
10+
11+
if __name__ == '__main__':
12+
print(__doc__)
13+
14+
filename = "data.json"
15+
data = {"name": "Åsa Äppelöstad"}
16+
17+
with open(filename, "w", encoding="utf-8") as f:
18+
json.dump(data, f, ensure_ascii=False)
19+
print("Saved UTF-8 characters to file: '{}'.".format(filename))
20+
21+
with open(filename, "r", encoding="utf-8") as f:
22+
dataRead = json.load(f)
23+
print("Loaded file '{}'.".format(filename))
24+
print(dataRead)

0 commit comments

Comments
 (0)