# -*- coding:utf-8 -*-
import pandas as pd
import collections, json
area_data = pd.read_csv('all.csv', encoding='utf8', sep=',') # gb18030
print(area_data.head())
indicator = area_data.columns[1:5].tolist()
print(indicator)
f = open("all.json", "w+", encoding='utf-8')
for i in range(len(area_data)):
area_dict = collections.OrderedDict() # 利用OrderedDict()建立有序词典
area_dict['question'] = str(area_data.loc[i, 'question'])
area_dict['answer'] = str(area_data.loc[i, 'answer'])
# area_dict['lat'] = str(area_data.loc[i, '经度'])
area_dict['options'] = indicator
value_list = area_data.iloc[i, 1:5].tolist()
value_list_new = [str(x) for x in value_list]
# value_list.append(area_data.iloc[i, 1:4].tolist().astype(str))
value_list.append(area_data.iloc[i, 1:5].tolist())
area_dict['values'] = value_list_new
# f.writelines(json.dumps(area_dict, ensure_ascii=False) + '\n')
f.write(json.dumps(area_dict, ensure_ascii=False) + '\n')
# f.writelines(json.dumps(area_dict, ensure_ascii=False, indent=4) + ',\n')
f.close() # 将文件关闭
# 使用json模块将构造好的字典保存到文件中
# area_dict.encode("utf-8")