python输出fi的字符无效

2024-06-16 10:56:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个小剧本:

from numpy import *
import numpy as np
import scipy.spatial as spt

X= np.loadtxt('edm')
myfile = open('edm.txt','w')

V= spt.distance.pdist(X.T,'sqeuclidean')
P = spt.distance.squareform(V)  
print P
myfile.write(P)

这个矩阵:

           0  199.778354301   
201.857546133 0

如果我运行我的程序;我在终端上得到这个(根据“打印”):

[[     0.          80657.85977805]
 [ 80657.85977805      0.        ]]

但在我的输出文件中,我得到如下无效字符:

��������z°¶¡±Û@z°¶¡±Û@��������

你知道为什么吗? 谢谢


Tags: fromimportnumpytxtasnpscipyopen
2条回答

该文件包含矩阵中数字的二进制表示形式。你知道吗


$ od -t x1z edm.txt 
0000000 00 00 00 00 00 00 00 00 79 a1 a6 c1 1d b1 f3 40  ........y......@
0000020 79 a1 a6 c1 1d b1 f3 40 00 00 00 00 00 00 00 00  y......@........
0000040

您可以使用NumPysavetxt方法来保存数组,而不用担心编码问题。你知道吗

在文件里

>>> np.savetxt('edm.txt', x)   # x is an array

相关问题 更多 >