[英]Saving Python Results as .txt File
I have this code ( unique_set=np.random.choice([0, 1], (10000, 10, 10, 10))
) that generates 10000 3D binary matrices and I'm attempting to save the result as a.txt file.我有这个代码( unique_set=np.random.choice([0, 1], (10000, 10, 10, 10))
)生成 10000 3D 二进制矩阵,我正在尝试将结果保存为 a.txt 文件. The other similar questions I checked were either trying to write a print statement to a file or were noticeably different.我检查的其他类似问题要么试图将打印语句写入文件,要么明显不同。 I tried so many of the solutions like the one below, but none of them worked.我尝试了很多像下面这样的解决方案,但都没有奏效。
sys.stdout = open("test.txt", "w")
print(unique_set)
sys.stdout.close()
Not knowing how the format of your output file should look like, this is one possibility:不知道 output 文件的格式应该是什么样子,这是一种可能性:
np.savetxt("test.txt", unique_set.flatten(), delimiter=",")
Try this one试试这个
import numpy as np
file = open('D:\\yourpath\\filename.txt', 'w')
unique_set=np.random.choice([0, 1], (10000, 10, 10, 10))
file.write('%s\n' %unique_set)
You can store as JSON text file (preserves it being a 4D array) -- storing Numpy N dimensional arrays您可以存储为 JSON 文本文件(保留为 4D 数组)—— 存储 Numpy N 维 arrays
import json
with open('"test.txt"', 'w') as f:
json.dump(unique_set.tolist(), f)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.