02.01-a-tour-of-python
# 写文件
import numpy as np
f = open('data.txt', 'w')
a = np.array([5,8])
b = np.array([7,7])
f.write(str(a[1]))
f.write('\n')
f.write(str(b))
# f.write('2 3 4 5\n')
f.close()
f = open('data.txt', 'w')
for i in range (20,40,2):
print (i)
f.write(str(i))
f.write('\n')
f.close()
20
22
24
26
28
30
32
34
36
38
# 读文件
f = open('data.txt')
data = []
for line in f:
# print (line)
# print (line.split())
# for field in line.split():
# print(field)
data.append([int(field) for field in line.split()])
f.close()
print (data)
[[1, 2, 3, 4], [2, 3, 4, 5]]
import numpy as np
for row in data:
print (np.array(row))
[1 2 3 4]
[2 3 4 5]
# 删除文件
import os
os.remove('data.txt')