【发布时间】:2020-07-05 19:55:32
【问题描述】:
我是 Python 的初学者。这就是我想要做的:
import numpy as np
r10 = np.array([[i for i in range(0,10)],[i*10 for i in range(0,10)]]).T
r6 = np.array([[i for i in range(0,6)],[i*10 for i in range(0,6)]]).T
r_comb = np.array([[r10],[r6]]).T
np.savetxt('out.txt',r_comb)
使用 np.savetxt 给我以下错误,因为它只支持一维数组:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Programs\Python\Python38-32\lib\site-packages\numpy\lib\npyio.py in savetxt(fname, X, fmt, delimiter, newline, header, footer, comments, encoding)
1433 try:
-> 1434 v = format % tuple(row) + newline
1435 except TypeError:
TypeError: only size-1 arrays can be converted to Python scalars
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-88-c3147f076055> in <module>
----> 1 np.savetxt('out.txt',r_comb)
<__array_function__ internals> in savetxt(*args, **kwargs)
~\AppData\Local\Programs\Python\Python38-32\lib\site-packages\numpy\lib\npyio.py in savetxt(fname, X, fmt, delimiter, newline, header, footer, comments, encoding)
1434 v = format % tuple(row) + newline
1435 except TypeError:
-> 1436 raise TypeError("Mismatch between array dtype ('%s') and "
1437 "format specifier ('%s')"
1438 % (str(X.dtype), format))
TypeError: Mismatch between array dtype ('object') and format specifier ('%.18e %.18e')
有没有其他方法可以将变量 r_comb 的内容保存到 .txt 文件中,以便我可以将其用于其他绘图程序? 基本上,我希望文本文件看起来像这样:
0 0.0 0 0.0
1 0.1 1 0.1
2 0.2 2 0.2
3 0.3 3 0.3
4 0.4 4 0.4
5 0.5 5 0.5
6 0.6
7 0.7
8 0.8
9 0.9
显示文本文件内容的图像
【问题讨论】:
-
你不能这样做(只需拨打
savetxt)。结果会更难阅读!
标签: python arrays python-3.x numpy saving-data