将结果写入文件时,有些会转到下一行。python

将结果写入文件时,有些会转到下一行。python,python,Python,我正在将一个函数的输出写入一个文件,如果写入的数字末尾有一个e+01,它会将其中一个数字写入下一行。比如说 0.12605695 1.64761066 1.55001798 0.01785089 15.80005801 0.12231149 1.65899943 1.56369755 0.01511221 10.45653689 0.1238494 1.67704048 1.59633667 0.00687468 0.96146016 1.10437

我正在将一个函数的输出写入一个文件,如果写入的数字末尾有一个e+01,它会将其中一个数字写入下一行。比如说

 0.12605695   1.64761066   1.55001798   0.01785089  15.80005801
 0.12231149   1.65899943   1.56369755   0.01511221  10.45653689
 0.1238494   1.67704048  1.59633667  0.00687468  0.96146016
 1.10437450e-01   1.62918081e+00   1.56024230e+00   1.82792879e-03
 -2.05519064e+00
 1.04326863e-01   1.63545256e+00   1.58687599e+00  -4.50753949e-03
 -6.40408013e+00
每行应该有五个数字,这是我代码的一部分,我将数字写入一个文件

 kop=open('new.txt', 'w')
 results=PCA(kk)
 res=results.mu
 print results.mu
 kop.write(str(res)+'\n')
 kop.close() 
我怎样才能让他们只写一行而不写下一行呢?
我需要它们都在正确的行中,就像我在不同的代码段中使用numpy.genfromtxt时,它要求它们都在正确的列中一样。

假设
结果。mu
可以像普通列表一样进行迭代,此代码段应该通过使用以下命令来完成您需要的操作:


根据
result.mu的类型,您可以尝试使用
rstrip()


这就是答案,我只是不知道这是一个numpy数组

 kop=open('new.txt', 'w')
 results=PCA(kk)
 res=results.mu
 res=np.ndarray.tolist(res)
 print results.mu
 kop.write(str(res)+'\n')
 kop.close() 

什么是kop
?是普通档案吗?
results.mu的类型是什么?是的,kop只是kop=open('new.txt','w')。mu是一个数组,由matplotlibIs
results.mu的PCA函数生成。mu
是一个NumPy数组吗?请注意,
str()
是为控制台输出而设计的,不是结构化输出,可能会意外地改变其外观,使其在终端上看起来更好。啊,在PCA文档中,它说它是numdims数组,它是numpy数组。
 results=PCA(kk)
 res=results.mu
 print results.mu
 for i,y in enumerate(result.mu):
     result[i] = y.rstrip()
 kop.write(str(res)+'\n')
 kop.close()
 kop=open('new.txt', 'w')
 results=PCA(kk)
 res=results.mu
 res=np.ndarray.tolist(res)
 print results.mu
 kop.write(str(res)+'\n')
 kop.close()