import csv
import io
import numpy as np
from numpy.matlib import repmat
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()
a=[]
dict=city_infos[0]
for headers in sorted(dict.keys()):
a.append(headers)
header=a
with open('C:\\Users\\Administrator\\Desktop\\实训\\data\\anhui.csv','a',newline='',encoding='utf-8')as f2:
writer=csv.DictWriter(f2,fieldnames=header)
writer.writeheader()
writer.writerows(city_infos)
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\实训\txttocsv.py", line 10, in <module>
for headers in eval(sorted(dict.keys())):
AttributeError: 'str' object has no attribute 'keys'