-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathjson_file.py
More file actions
48 lines (38 loc) · 1.06 KB
/
Copy pathjson_file.py
File metadata and controls
48 lines (38 loc) · 1.06 KB
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
40
41
42
43
44
45
46
47
48
#!/usr/bin/python2
#
# http://www.forum-raspberrypi.de/Thread-python-neue-datei-erstellen-mit-python-2-7?pid=281304#pid281304
# http://stackoverflow.com/a/37795053/2641799
#
import time
import json
import os
import io
try:
to_unicode = unicode
except NameError:
to_unicode = str
gamefilespath = '/tmp/'
def new_gamefile(id, data):
with io.open(gamefilespath + id + '.json', 'w', encoding='utf8') as outfile:
str_ = json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False)
outfile.write(to_unicode(str_))
def read_gamefile(id):
with open(gamefilespath + id + '.json') as data_file:
return json.load(data_file)
# Usage:
daten = {
'Player1': {
'Name': 'meigrafd',
'Score': 123,
},
'Player2': {
'Name': 'hamyam',
'Score': 321,
},
}
new_gamefile('987', daten)
print time.ctime( os.path.getctime(gamefilespath + '987' + '.json') )
game_data = read_gamefile('987')
print game_data['Player1']
print game_data['Player1']['Name']
print game_data['Player1']['Score']