如何在 python 中保存数据文件
How to save a datafile in python
他们让我在学校制作一个程序,将骰子掷 100 次,将其保存到文件中,然后绘制结果图表。我已经制作了程序但我无法保存它,我试过了但是我看不到文件,有人可以帮我吗?
这是我的代码:
import random
import pickle
def dado(n):
result = []
for i in range(0,n):
result.append(random.randint(1, 6))
return result
print(dado(100))
#save the list
pickle.dump(dado, open("dado.txt", "wb"))
这个存档程序可能喜欢这样
import random
import pickle
def dado(n):
result = []
for i in range(0,n):
result.append(random.randint(1, 6))
return result
print(dado(100))
#save the list
pickle.dump(dado(100), open("dado.pkl", "wb"))
或
import random
import numpy as np
def dado(n):
result = []
for i in range(0,n):
result.append(random.randint(1, 6))
return result
print(dado(100))
#save the list
np.set_printoptions(suppress=True)
np.savetxt("dado.txt", dado(100), fmt='%.1f')
他们让我在学校制作一个程序,将骰子掷 100 次,将其保存到文件中,然后绘制结果图表。我已经制作了程序但我无法保存它,我试过了但是我看不到文件,有人可以帮我吗? 这是我的代码:
import random
import pickle
def dado(n):
result = []
for i in range(0,n):
result.append(random.randint(1, 6))
return result
print(dado(100))
#save the list
pickle.dump(dado, open("dado.txt", "wb"))
这个存档程序可能喜欢这样
import random
import pickle
def dado(n):
result = []
for i in range(0,n):
result.append(random.randint(1, 6))
return result
print(dado(100))
#save the list
pickle.dump(dado(100), open("dado.pkl", "wb"))
或
import random
import numpy as np
def dado(n):
result = []
for i in range(0,n):
result.append(random.randint(1, 6))
return result
print(dado(100))
#save the list
np.set_printoptions(suppress=True)
np.savetxt("dado.txt", dado(100), fmt='%.1f')