forked from SZP1206/YetAnotherValuesTest.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv2questionsjs.py
39 lines (35 loc) · 1.08 KB
/
csv2questionsjs.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
import pandas as pd
import sys
csv_file = 'question-value.csv'
question_js_file = 'questions.js'
question_name = '内容'
econ_name = '平等'
dipl_name = '世界'
govt_name = '自由'
scty_name = '进步'
envo_name = '生态'
if __name__ == '__main__':
df = pd.read_csv(csv_file)
df.fillna(0, inplace=True)
print(df)
default_encoding = sys.getdefaultencoding()
questions = []
for _, row in df.iterrows():
if row[question_name] == 0:
continue
questions.append(
{
"question": row[question_name].encode(default_encoding).decode('utf-8'),
"effect": {
"econ": int(row[econ_name]),
"dipl": int(row[dipl_name]),
"govt": int(row[govt_name]),
"scty": int(row[scty_name]),
"envo": int(row[envo_name]),
}
}
)
json_str = json.dumps(questions, ensure_ascii=False, indent=4)
with open(question_js_file, 'w') as f:
f.write('questions = ' + json_str + ';')