我已经做了很多搜索,但我很难找到解决我的具体问题的方法。我相信这是一个非常简单的解决方案。
我正在从卡片游戏领域的最佳代理商那里接受其他代理商的比赛。代理人经历了几代人的演变。所以在每次迭代中我都想做以下事情:
创建名为“results + generation number”的文件,即results2
然后我想在新行上添加每个函数调用的输出
播放x =输出1的结果
播放y = outputs2
所以我确实找到了问题的解决方案,但我遇到了另一个问题。当我运行程序一次时,除了文件名之外什么也没有保存。第二次,结果来自第一次等等。
f = open("results_{0}.txt".format(counter),'w')
f.write("Agent A vs. Champion\n"+"Champion wins = "+str(winsA1)+" Agent A wins = "+str(winsB1))
f.write("\n\nAgent B vs. Champion\n"+"Champion wins = "+str(winsA2)+" Agent B wins = "+str(winsB2))
f.write("\n\nRandom vs. Champion\n"+"Champion wins = "+str(winsA3)+" Random wins = "+str(winsB3))
f.close
答案 0 :(得分:0)
您希望使用pickle
保存对象,import pickle # or import cPickle as pickle
# Create dictionary, list, etc.
favorite_color = { "lion": "yellow", "kitty": "red" }
# Write to file
f_myfile = open('myfile.pickle', 'wb')
pickle.dump(favorite_color, f_myfile)
f_myfile.close()
# Read from file
f_myfile = open('myfile.pickle', 'rb')
favorite_color = pickle.load(f_myfile) # variables come out in the order you put them in
f_myfile.close()
将对象表示为字符串,以便保存。这是sample code:
{{1}}