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

How to write list of integers into file with Python 2.7?

本问题已经有最佳答案,请猛点这里访问。

我有清单

1
[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

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

但得到这样的人物

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
(lp0
I4
aI5
aI7
aI8
aI10
aI11
aI13
aI14
aI16
aI17
aI19
aI21
aI22
aI24


使用json模块:

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