import csv
import io
import numpy as np
from numpy.matlib import repmat
import demjson
import re
path=open('C:\\Users\\Administrator\\Desktop\\实训\\data\\beijing.txt','r',encoding='utf-8')
with open('C:\\Users\\Administrator\\Desktop\\实训\\data\\beijing.txt','r',encoding='utf-8')as f1:
city_infos=f1.read()
filednames=['cityName','currentConfirmedCount', 'confirmedCount','suspectedCount','curedCount','deadCount','highDangerCount','midDangerCount','locationId','currentConfirmedCountStr']
city_dict=demjson.decode(city_infos)
with open('C:\\Users\\Administrator\\Desktop\\实训\\data\\beijing.csv','w',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=demjson.decode(city_infos)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\demjson.py", line 5696, in decode
result = j.decode( txt,
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\demjson.py", line 4915, in decode
raise errors[0]
demjson.JSONDecodeError: ('Unknown identifier', 'True')
文件内容:
[{'cityName': '朝阳区', 'currentConfirmedCount': 525, 'confirmedCount': 605, 'suspectedCount': 0, 'curedCount': 80, 'deadCount': 0, 'highDangerCount': 0, 'midDangerCount': 0, 'locationId': 110105, 'currentConfirmedCountStr': '525'}, {'cityName': '丰台区', 'currentConfirmedCount': 444, 'confirmedCount': 730, 'suspectedCount': 0, 'curedCount': 286, 'deadCount': 0, 'highDangerCount': 0, 'midDangerCount': 0, 'locationId': 110106, 'currentConfirmedCountStr': '444'}, {'cityName': '房山区', 'currentConfirmedCount': 314, 'confirmedCount': 334, 'suspectedCount': 0, 'curedCount': 20, 'deadCount': 0, 'highDangerCount': 0, 'midDangerCount': 0, 'locationId': 110111, 'notShowCurrentConfirmedCount': True, 'currentConfirmedCountStr': '-'}, {'cityName': '境外输入', 'currentConfirmedCount': 283, 'confirmedCount': 755, 'suspectedCount': 3, 'curedCount': 472, 'deadCount': 0, 'highDangerCount': 0, 'midDangerCount': 0, 'locationId': 0, 'currentConfirmedCountStr': '283'}, {'cityName': '海淀区', 'currentConfirmedCount': 276, 'confirmedCount': 358, 'suspectedCount': 0, 'curedCount': 82, 'deadCount': 0, 'highDangerCount': 0, 'midDangerCount': 0, 'locationId': 110108, 'currentConfirmedCountStr': '276'}, {'cityName': '通州区', 'currentConfirmedCount': 95, 'confirmedCount': 115, 'suspectedCount': 0, 'curedCount': 20, 'deadCount': 0, 'highDangerCount': 1, 'midDangerCount': 0, 'locationId': 110112, 'currentConfirmedCountStr': '95'}, {'cityName': '东城区', 'currentConfirmedCount': 64, 'confirmedCount': 83, 'suspectedCount': 0, 'curedCount': 19, 'deadCount': 0, 'highDangerCount': 0, 'midDangerCount': 0, 'locationId': 110101, 'currentConfirmedCountStr': '64'}, {'cityName': '昌平区', 'currentConfirmedCount': 62, 'confirmedCount': 112, 'suspectedCount': 0, 'curedCount': 50, 'deadCount': 0, 'highDangerCount': 1, 'midDangerCount': 1, 'locationId': 110114, 'notShowCurrentConfirmedCount': True, 'currentConfirmedCountStr': '-'}, {'cityName': '顺义区', 'currentConfirmedCount': 57, 'confirmedCount': 102, 'suspectedCount': 0, 'curedCount': 45, 'deadCount': 0, 'highDangerCount': 0, 'midDangerCount': 0, 'locationId': 110113, 'currentConfirmedCountStr': '57'}, {'cityName': '西城区', 'currentConfirmedCount': 56, 'confirmedCount': 115, 'suspectedCount': 0, 'curedCount': 59, 'deadCount': 0, 'highDangerCount': 0, 'midDangerCount': 0, 'locationId': 110102, 'currentConfirmedCountStr': '56'}, {'cityName': '大兴区', 'currentConfirmedCount': 34, 'confirmedCount': 168, 'suspectedCount': 0, 'curedCount': 134, 'deadCount': 0, 'highDangerCount': 0, 'midDangerCount': 0, 'locationId': 110115, 'notShowCurrentConfirmedCount': True, 'currentConfirmedCountStr': '-'}, {'cityName': '石景山区', 'currentConfirmedCount': 19, 'confirmedCount': 34, 'suspectedCount': 0, 'curedCount': 15, 'deadCount': 0, 'highDangerCount': 0, 'midDangerCount': 0, 'locationId': 110107, 'currentConfirmedCountStr': '19'}, {'cityName': '经济开发区', 'currentConfirmedCount': 14, 'confirmedCount': 14, 'suspectedCount': 0, 'curedCount': 0, 'deadCount': 0, 'highDangerCount': 0, 'midDangerCount': 0, 'locationId': 0, 'notShowCurrentConfirmedCount': True, 'currentConfirmedCountStr': '-'}, {'cityName': '门头沟区', 'currentConfirmedCount': 13, 'confirmedCount': 18, 'suspectedCount': 0, 'curedCount': 5, 'deadCount': 0, 'highDangerCount': 0, 'midDangerCount': 0, 'locationId': 110109, 'currentConfirmedCountStr': '13'}, {'cityName': '延庆区', 'currentConfirmedCount': 7, 'confirmedCount': 8, 'suspectedCount': 0, 'curedCount': 1, 'deadCount': 0, 'highDangerCount': 2, 'midDangerCount': 4, 'locationId': 110119, 'currentConfirmedCountStr': '7'}, {'cityName': '密云区', 'currentConfirmedCount': 3, 'confirmedCount': 10, 'suspectedCount': 0, 'curedCount': 7, 'deadCount': 0, 'highDangerCount': 0, 'midDangerCount': 0, 'locationId': 110118, 'notShowCurrentConfirmedCount': True, 'currentConfirmedCountStr': '-'}, {'cityName': '外地来京', 'currentConfirmedCount': 2, 'confirmedCount': 27, 'suspectedCount': 0, 'curedCount': 25, 'deadCount': 0, 'highDangerCount': 0, 'midDangerCount': 0, 'locationId': 0, 'notShowCurrentConfirmedCount': True, 'currentConfirmedCountStr': '-'}, {'cityName': '怀柔区', 'currentConfirmedCount': 1, 'confirmedCount': 9, 'suspectedCount': 0, 'curedCount': 8, 'deadCount': 0, 'highDangerCount': 0, 'midDangerCount': 0, 'locationId': 110116, 'notShowCurrentConfirmedCount': True, 'currentConfirmedCountStr': '-'}, {'cityName': '待明确地区', 'currentConfirmedCount': -2243, 'confirmedCount': 99, 'suspectedCount': 0, 'curedCount': 2333, 'deadCount': 9, 'highDangerCount': 0, 'midDangerCount': 0, 'locationId': -1, 'currentConfirmedCountStr': '-2243'}]