c = [{"城市":"SH","日期":"2020/01/01","温度":25,"风力":5},\
{"城市":"BJ","日期":"2022/02/02","温度":29,"风力":4}]
b = open("xr.txt","w")
for i in c:
b.write(str(i) + "\n")
b.close()
这个是输出结果
想要达到的效果:
c = [{"城市":"SH","日期":"2020/01/01","温度":25,"风力":5},\
{"城市":"BJ","日期":"2022/02/02","温度":29,"风力":4}]
b = open("xr.txt","w")
for i in c:
b.write(str(i) + "\n")
b.close()
这个是输出结果
想要达到的效果:
c = [{"城市":"SH","日期":"2020/01/01","温度":25,"风力":5},\
{"城市":"BJ","日期":"2022/02/02","温度":29,"风力":4}]
b = open("xr.txt","w")
b.write('ID\t日期\t\t城市\t温度\t风力\n')
for ind, val in enumerate(c):
b.write(f"{ind}\t{val['日期']}\t{val['城市']}\t{val['温度']}\t{val['风力']}\n")
b.close()