简体   繁体   English

数组的最大值不会将ASCII文本保存到文件

[英]Max value of array won't save ASCII text to file

I am reading points from a frequency spectrum graph into an array like this: 我正在从频谱图中读取点到这样的数组:

self.iq_fft = self.dofft(self.iq)#Gets values of all data point in block (in dB)
x = self.iq_fft #puts values into x for simplicity
maxi = max(x)
print maxi #This part works fine

def dofft(self, iq):
    N = len(iq)
    iq_fft = scipy.fftpack.fftshift(scipy.fft(iq))      

Everything works great and it prints out the maximum value of the array to the screen perfect, however, when I try to save the max value to a file, like this: 一切都很好,它打印出完美的数组的最大值,但是,当我尝试将最大值保存到文件时,如下所示:

savetxt('myfilelocation', maxi, fmt='%1.4f')#saves max(x) in binary format

It ends up saving the binary value to the text file and not the nice pretty ASCII one that printed to the screen. 它最终将二进制值保存到文本文件中,而不是打印到屏幕上的漂亮的ASCII。 Funny thing is, when I just dump the whole array to a file it looks fine. 有趣的是,当我将整个数组转储到文件时,它看起来很好。 Any ideas on how to fix this? 有想法该怎么解决这个吗?

numpy.savetxt saves an array to a file, and it looks like maxi is a python Float. numpy.savetxt数组保存到文件中,看起来maxi是一个python Float。 It's sort of unusual to use savetxt to save a scalar (why not just use Python's built-in file i/o functions?), but if you must, this might work: 使用savetxt保存标量有savetxt不寻常(为什么不使用Python的内置文件i / o函数?),但如果必须,这可能有效:

savetxt('myfilelocation', [maxi], fmt='%1.4f')

Notice that I've put maxi in a list. 请注意,我已将maxi放入列表中。 Also, make sure that myfilelocation doesn't end with a .gz . 另外,请确保myfilelocation不以.gz结尾。 If it does, numpy will compress it. 如果是这样,numpy会压缩它。 When it doubt, just use .txt , or no extension at all. 当它有疑问时,只需使用.txt ,或根本不使用扩展名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM