Python 3.x 未使用Python将完整值写入文件
我的想法是将一些大量的位写入一个文件(几乎64*4800位)。它在写,但不是全部 控制台输出如下所示 [1.1.0….1.0.1] 如果我减少要保存的位数,那么它就会工作 我将把我的代码粘贴到这里。此代码正在将模拟采样为数字Python 3.x 未使用Python将完整值写入文件,python-3.x,numpy-ndarray,Python 3.x,Numpy Ndarray,我的想法是将一些大量的位写入一个文件(几乎64*4800位)。它在写,但不是全部 控制台输出如下所示 [1.1.0….1.0.1] 如果我减少要保存的位数,那么它就会工作 我将把我的代码粘贴到这里。此代码正在将模拟采样为数字 y= function(x) # Inside this function I am generating binary values and stored to y ################ y is in numpy.ndarray form #######
y= function(x) # Inside this function I am generating binary values and stored to y
################ y is in numpy.ndarray form
################ x is a sine wave
f=open('filename.txt',"w+")
f.write(str(y)) #we have to convert the numpy.ndarray to str.
f.close()
当我打开我的filename.txt
文件时,它将二进制值显示为
[1.1.0….1.0.1]
这与控制台中的相同
请帮我解决这个问题。我需要保存在文件中的所有位(64*4800)首先尝试将numpy数组转换为列表:
y = function(x) # Inside this function I am generating binary values and stored to y
################ y is in numpy.ndarray form
################ x is a sine wave
y_list = y.tolist() # Convert to python list
# use the with context manager and you don't need to call .close() explicitly
with open('filename.txt',"w+") as f:
f.write(str(y_list)) #we have to convert the numpy.ndarray to a list and then to str(y_list) which will write the entire bits.
正如你所说,我更改了代码。但输出仍然是一样的(我可以看到函数(x)的代码吗?我怀疑它实际上是在将您输出的内容返回到文件中,并且存在缺陷。我已将f.write(str(y))更改为f.write(str(y_列表))。我们没有传递转换后的列表。这就是问题所在。我编辑了它并接受了您的答案。谢谢