-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson2table.py
72 lines (60 loc) · 1.26 KB
/
json2table.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
import json
import sys
reload(sys)
sys.setdefaultencoding('utf8')
f = open(sys.argv[1])
d = json.load(f)
f.close()
cols = (
"average",
"boardgameartist",
"boardgamecategory",
"boardgamedesigner",
"boardgamefamily",
"boardgamemechanic",
"boardgamepublisher",
"maxplayers",
"maxplaytime",
"minage",
"minplayers",
"minplaytime",
"n_comments",
"numcomments",
"owned",
"playingtime",
"usersrated",
"wanting",
"trading",
"wishing",
"numweights",
"averageweight",
)
out1 = open("mdata.games.tsv", "w")
out3 = open("comments.users.tsv", "w")
out1.write("\t".join(["id"] + [col for col in cols]) + "\n")
out3.write("\t".join(("id", "rating", "username", "text")) + "\n")
for k,v in d.iteritems():
line = "\t".join(
[k] +
[
",".join(v.get(col, "NA")).replace("\t", " ")
if isinstance(v.get(col),list)
else str(v.get(col, "NA"))
for col in cols
]
)
out1.write(line + "\n")
comments = v.get("comments", [])
for comment in comments:
out3.write("\t".join((
k,
comment["rating"],
comment["username"],
comment["value"].replace("\n"," ").replace("\t", " ")
if comment["value"]
else "NA",
))
+ "\n")
out1.close()
out3.close()