查了一下是json不支持单引号,但把引号全部替换了还是报错。
import csv
import io
import numpy as np
from numpy.matlib import repmat
import json
path=open('C:\\Users\\Administrator\\Desktop\\实训\\data\\anhui.txt','r',encoding='utf-8')
with open('C:\\Users\\Administrator\\Desktop\\实训\\data\\anhui.txt','r',encoding='utf-8')as f1:
city_infos=f1.read()
filednames=['cityName','currentConfirmedCount', 'confirmedCount','suspectedCount','curedCount','deadCount','highDangerCount','midDangerCount','locationId','currentConfirmedCountStr']
city_infos = city_infos.replace("'", '"')
city_dict=json.loads(city_infos)
with open('C:\\Users\\Administrator\\Desktop\\实训\\data\\anhui.csv','a',newline='',encoding='utf-8')as f2:
writer=csv.DictWriter(f2,filednames)
writer.writeheader()
writer.writerows(city_dict)
print("success")
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\实训\txttocsv.py", line 11, in <module>
city_dict=json.loads(city_infos)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\json\decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 644 (char 643)