Worflow将结果写入Python文件

我正在Python中运行几个模拟。 每个模拟都有结果数据: name, time, cost, error ,其中name是实际模拟的string,time是一个intvariables,cost是一个numpy数组或列表,每个模拟的大小不同,错误是一个floatvariables。 到目前为止,我总是只进行一些模拟。 我将结果存储在单独的.txt文件中。 但现在我需要运行多达100个模拟,我认为这是处理它的最佳时机:)。

我可以将模拟结果存储在例如字典中:

 result = {"SimulationA": {"time": 458,"cost": [12.35, 1.15, 66, 85], "error": 2.45 }"SimulationB":{"time": 512,"cost": [12.35, 66], "error": 12.3 } } 

如果可能的话,我想把所有东西都写入一个excel或csv文件中,结构如下:

在这里输入图像说明

结果如下:

在这里输入图像说明

你会推荐什么样的工作stream程(我宁愿简单些,计算时间等也不是很重要)? 非常感谢您的宝贵时间。

如果模拟顺序没有改变,为什么不把信息存储在一个像[[time1,cost1,error1],[time2,cost2,error2],…]的numpy数组中,然后使用numpy来保存它作为一个CSV文件。 然后,你只需要打开文件在Excel和瞧。

简单的代码和excel结果的图片。 您可以随心所欲地将其包含在csv文件中,如果您更喜欢所显示的格式,或者在随机参数中交换100和3,则也可以将数据转置为Excel。 这也可能是最简单的列表,然后将数据转换为numpy数组来保存它。

https://imgur.com/EpGKlaK

 import numpy as np data = np.random.rand(100,3) np.savetxt('test.csv',data, delimiter=',') 

你花费的额外的逗号是有问题的,所以我把它做成了pipe道。

 result = {"SimulationA": {"time": 458,"cost": [12.35, 1.15, 66, 85], "error": 2.45 }, "SimulationB":{"time": 512,"cost": [12.35, 66], "error": 12.3 } } keys = result.keys() rows = [ ',' + ','.join(result.keys()), "time," + ','.join([ str(result[x]['time']) for x in keys]), "cost," + ','.join([ '|'.join([ str(y) for y in result[x]['cost']]) for x in keys]), "error," + ','.join([ str(result[x]['error']) for x in keys]) ] to_write = '\n'.join(rows) with open('myfile.csv', 'w+') as f: f.write(to_write)