将输出保存为networkx中的文本文件

时间:2013-06-13 06:06:53

标签: python networkx

我在python shell中打印了一个输出,如:

>>>{1: 117.33282674772036, 2: 119.55324074074075, 3: 116.45497076023392, 4:          113.77561475409836, 5: 112.93896713615024, 6: 114.23583333333333, 7: 124.92402972749794, 8: 121.40603448275863, 9: 116.4946452476573, 10: 112.89107142857142, 11: 122.33312577833125, 12: 116.57083333333334, 13: 122.2856334841629, 14: 125.26688815060908, 15: 129.13817204301074, 16: 128.78991596638656, 17: 127.54600301659126, 18: 133.65972222222223, 19: 127.28315789473685, 20: 125.07205882352942, 21: 124.79464285714286, 22: 131.36170212765958, 23: 130.17974002689377, 24: 138.37055555555557, 25: 132.72380952380954, 26: 138.44230769230768, 27: 134.82251082251082, 28: 147.12448979591838, 29: 149.86879730866275, 30: 145.04521072796936, 31: 143.72442396313363, 32: 148.12940140845072, 33: 140.06355218855219, 34: 145.44537815126051, 35: 146.50366300366301, 36: 146.2173611111111, 37: 152.36319881525361, 38: 156.42249459264599, 39: 154.6977564102564, 40: 155.47647058823529, 41: 158.72357723577235, 42: 162.23746031746032, 43: 149.30991931656382, 44: ........

它代表邻近的邻居。如何在python中逐行将此输出保存在文本文件中? 像:

1:117.3328268788
2:119.5532822788

1 个答案:

答案 0 :(得分:3)

这样的事情:

with open('some_file.txt', 'w') as f:
   for k in sorted(your_dic):
      f.write("{}:{}\n".format(k, your_dic[k]))