forked from bimzcy/rank4douban
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_data.py
66 lines (53 loc) · 2.07 KB
/
build_data.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
# !/usr/bin/python3
# -*- coding: utf-8 -*-
# Copyright (c) 2017-2020 Rhilip <[email protected]>
"""
This script do this things:
1. convert raw data csv to snippets json.
2. merge all json file from snippets to generation the out json file `data.json`
"""
import os
import csv
import json
import time
data_dir = os.path.join(".", "data")
snippets_descr_csv = os.path.join(data_dir, "00_snippets.csv")
snippets_dir = os.path.join(".", "snippets")
out_json = os.path.join(".", "data.json")
def convert():
with open(snippets_descr_csv, encoding='utf-8') as f:
data_csv_descr = list(csv.DictReader(f))
for raw_data in data_csv_descr:
data_csv_file = os.path.join(data_dir, raw_data["file"])
data_csv = csv.DictReader(open(data_csv_file, encoding='utf-8'))
rank_row = data_csv.fieldnames[0]
data_list = {row["dbid"]: row[rank_row] for row in data_csv}
json_data = {
"title": raw_data["title"],
"short_title": raw_data["short_title"],
"href": raw_data["href"],
"top": int(raw_data["file"][:2]),
"list": data_list
}
if raw_data["other"]:
other = (raw_data["other"] + "|").split("|")
for i in [i for i in other if i != ""]:
k, v = i.split(":")
if k and v:
json_data[k] = v
json_file = os.path.join(snippets_dir, raw_data["file"].replace(".csv", ".json"))
with open(json_file, 'w', encoding='utf-8') as f:
json.dump(json_data, f, indent=3, sort_keys=True, ensure_ascii=False)
def merge():
temp_dict = {}
for file in os.listdir(snippets_dir):
snippet_file = os.path.join(snippets_dir, file)
snippet_name = file[3:-5]
with open(snippet_file, 'r', encoding='utf-8') as f:
temp_snippet = json.load(f)
temp_dict[snippet_name] = temp_snippet
with open(out_json, 'w', encoding='utf-8') as f:
json.dump(temp_dict, f, indent=3, sort_keys=True, ensure_ascii=False)
if __name__ == '__main__':
convert()
merge()