利用python将csv(UTF-8编码)转化为json格式,方便后续使用,但是转化为json格式后,在使用时会UFT-8报错,不知道怎么回事
csv转换成json的代码如下
```python
# -*- coding:utf-8 -*-
import pandas as pd
import numpy as np
import json
import collections
first_data = pd.read_csv('F:\pachong\CNKI_抖振报警.csv', encoding='UTF-8')
print(first_data.head())
f = open("报警优化.json", "w+")
for i in range(len(first_data)):
data_dict = collections.OrderedDict() ##利用OrderedDict()建立有序词典
data_dict['article'] = first_data.loc[i, '文章']
data_dict['author'] = first_data.loc[i, '一作']
data_dict['all_author'] = first_data.loc[i, '所有作者']
data_dict['source'] = first_data.loc[i, '来源']
data_dict['time'] = str(first_data.loc[i, '时间'])
data_dict['database'] = first_data.loc[i, '数据库']
data_dict['keywords'] = first_data.loc[i, '关键字']
data_dict['abstract'] = first_data.loc[i, '摘要']
data_dict['direction'] = first_data.loc[i, '研究方向']
data_dict['link'] = first_data.loc[i, '链接']
# 使用json模块将构造好的字典保存到文件中
# data_dict.encode("utf-8")
f.writelines(json.dumps(data_dict, ensure_ascii=False, indent=4) + ',\n')
f.close() # 将文件关闭
此处代码参考:https://blog.csdn.net/ziyin_2013/article/details/113779616```
生成的json文件内容如下(截取一部分)
```python
{
"article": "啊",
"author": "啊",
"all_author": "啊",
"source": "啊",
"time": "啊",
"database": "啊",
"keywords": "啊",
"abstract": "啊",
"direction": "啊",
"link": "啊"
},
{
"article": "啊",
"author": "啊",
"all_author": "啊",
"source": "啊",
"time": "啊",
"database": "啊",
"keywords": "啊",
"abstract": "啊",
"direction": "啊",
"link": "啊"
}
报错内容如下:
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 1 (char 2)
```