如何用Python 2.7将整数列表写入文件?

时间:2016-03-08 15:35:23

标签: python list io

我有清单

[4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 21, 22, 24, 25, 36, 39, 40, 43, 45, 46, 48, 49, 55, 57, 58, 60, 61, 64, 68, 71, 72, 74, 75, 77, 78, 80, 81, 82, 84, 86, 87, 89, 90, 92, 93, 95, 96, 98, 99, 101, 102, 104, 105, 107, 108, 110, 112, 113, 115, 116, 118, 122, 125, 127, 128, 130, 131, 133, 134, 136, 137, 139, 140]

我尝试了pickle

with open('m1.dat', 'wb') as f:
    pickle.dump(matches1, f)

但是有这样的角色

(lp0
I4
aI5
aI7
aI8
aI10
aI11
aI13
aI14
aI16
aI17
aI19
aI21
aI22
aI24

1 个答案:

答案 0 :(得分:1)

使用json模块:

import json
var = [1,4,5]
with open("file_path","w") as wfile:
     json.dump(var,wfile)