0

我想从控制台将小波系数放入 .csv 行。

下面的代码只能将一位数字放入 .csv 文件

   def waveletdbbiorone(self):     #function for Wavelets computation
     for filename in glob.iglob ('*.tif'):
     imgwbior = mahotas.imread (filename) #read the image
     arraywbior = numpy.array([imgwbior])#make an array for pywt module
     coefwbior = pywt.wavedec(arraywbior,'db1')#compute wavelet coefficients
     arr = numpy.array([coefwbior])
     np.set_printoptions(threshold=3)
     # print arr
  for elem in arr.flat[:50]:
    #print('{}\t'.format(elem))  #this code puts the result to console
    with open('сomput.csv', 'wb') as csvfile:
      writer = csv.writer(csvfile, delimiter=' ', quotechar='|',    quoting=csv.QUOTE_MINIMAL)
      writer.writerow([('{}\t'.format(elem))])

谢谢

4

1 回答 1

2

我认为问题在于您始终使用w(写入模式)打开文件,这会自动删除内容:

尝试这个:

 with open('сomput.csv', 'wb') as csvfile:
     for elem in arr.flat[:50]:
        #print('{}\t'.format(elem))  #this code puts the result to console

         writer = csv.writer(csvfile, delimiter=' ', quotechar='|',    quoting=csv.QUOTE_MINIMAL)
         writer.writerow([('{}\t'.format(elem))])

另外,保存数组的首选方法:

numpy.savetxt("foo.csv", array, delimiter=" ")

和展平也有效:

numpy.savetxt("foo.csv", array.flat[:50], delimiter=" ")
于 2013-06-30T11:51:09.893 回答