方法一
import copy
dict_list = []
dict_list.append(copy.deepcopy(write_dict))
result = pd.DataFrame.from_dict(dict_list)
result.to_csv('policy_based.csv', index=False)
方法二
import csv
output_file = 'economic.csv'
try:
with open(output_file, 'w', newline='') as csvfile:
fieldnames = ['date', 'total_cost', 'total_income', 'total_revenue', 'station_name', 'station_id']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
data_dict = {'date': data_els[0, 1], 'total_cost': system_cost, 'total_income': system_income, 'total_revenue': system_revenue, 'station_name': station_name, 'station_id': station_id}
print(data_dict)
writer.writerow(data_dict)
logging.info('输出数据成功写入CSV文件')
except Exception as e:
logging.error('输出数据写入CSV文件失败,错误原因为{}'.format(e))
return 107