使用python将二维numpy ndarray写入到文件中
a = np.random.random((5, 4))
# ndarray转为list
b = a.tolist()
# 二维数组遍历,列表生成式
c = [[i for i in j] for j in b]
with open("a.txt", "w") as fp:
# 二维数组遍历,列表生成式, 省去写for循环
# "n".join表示把每一行之后加入换行符
# " ".join表示把每一行不同字符之间加入空格
fp.writelines("n".join([" ".join([str(i) for i in j]) for j in b]))
a.txt的结果



