我已经在网站上搜索过了,但没有找到任何与我的案件有关的具体信息。 我有一个从文本文件创建数组的代码,我只想创建一个与输入文件类似的新文件,但是在右边的列中添加了新数组。我该怎么办?
输入文件示例:
4.00171 2.04336E14 14.31034 0.65049
2.56491 6.89220E13 13.83835 1.05022
1.95019 3.45559E13 13.53852 1.38901
创建数组的代码的一部分:
import numpy as np
dataset = np.genfromtxt(fname='input.dat')
s=dataset[:,3]
d=1.686/s
我只想将数组d添加到输入文件右侧的列中,并将所有内容保存到一个新的输出文件中。(我使用的是Python2.7)。
通过拆分换行符将文件读入行数组。把线绕过去。在将每一行写入新文件之后,追加“d”数组的下一个元素(加上一个换行符)。
使用
numpy.append
和numpy.savetxt
函数:output.txt
内容如下:为什么不沿着正确的轴
concatenate
?问题是变量
d
和s
都具有(3,)
形状,因此它们更像一个向量而不是一个数组。以下解决问题:现在你只需要把你的数据输出回一个文件,你就完成了。。。
np.savetxt('example.txt', output)
相关问题 更多 >
编程相关推荐