简体   繁体   English

如何使用Python将数组中的数字从傅立叶变换输出到Excel文件中的一行

[英]How to output the numbers in array from Fourier transform to a one single line in Excel file using Python

I want to output the numeric result from image processing to the .xls file in one line exactly in cells horizontally, does anybody can advice what Python module to use and what code to add, please? 我想将图像处理的数值结果精确地水平输出到.xls文件中的一行中,请问有人可以建议使用哪个Python模块以及添加什么代码吗? In other words, how to arrange digits from an array and put them exactly in Excel cells horizontally? 换句话说,如何排列数组中的数字并将它们精确地水平放置在Excel单元格中?

Code fragment:
def fouriertransform(self):     #function for FT computation
  for filename in glob.iglob ('*.tif'):
     imgfourier = mahotas.imread (filename) #read the image
     arrayfourier = numpy.array([imgfourier])#make an array 
     # Take the fourier transform of the image.
     F1 = fftpack.fft2(imgfourier)
     # Now shift so that low spatial frequencies are in the center.
     F2 = fftpack.fftshift(F1)
     # the 2D power spectrum is:
     psd2D = np.abs(F2)**2
     print psd2D
     f.write(str(psd2D))#write to file

This should be a comment, but I don't have enough rep. 这应该是一条评论,但是我没有足够的代表。

What other people say 别人怎么说

It looks like this is a duplicate of Python Writing a numpy array to a CSV File , which uses numpy.savetxt to generate a csv file (which excel can read) 看起来这是Python的副本,它将numpy数组写入CSV文件 ,该文件使用numpy.savetxt生成一个csv文件(excel可以读取)

Example

Numpy is probably the easiest way to go in my opinion. 在我看来,Numpy可能是最简单的方法。 You could get fancy and use the np.flatten module. 您可能会喜欢上np.flatten模块。 After that, it would be as simple as saving the array with a comma separating each element. 之后,就像用逗号分隔每个元素来保存数组一样简单。 Something like this (tested!) is even simpler 这样的东西(经过测试!)更简单

import numpy as np

arr = np.ones((3,3))
np.savetxt("test.csv",arr,delimiter=',',newline=',')

Note this is a small hack, since newlines are treated as commas. 请注意,这是一个小技巧,因为换行符被视为逗号。 This makes "test.csv" look like: 这使“ test.csv”看起来像:

1,1,1,1,1,1,1,1,1, # there are 9 ones there, I promise! 1,1,1,1,1,1,1,1,1, #我保证有9个! 3x3 3x3

Note there is a trailing comma. 请注意有一个逗号结尾。 I was able to open this in excel no problem, voila! 我可以用excel打开这个,没问题,瞧!

在此处输入图片说明

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

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