1

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? In other words, how to arrange digits from an array and put them exactly in Excel cells horizontally?

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
Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
KvasDub
  • 281
  • 7
  • 16
  • 3
    Read about Python's csv module. You can write a csv file and read it in Excel. – James Thiele May 13 '13 at 16:19
  • Thank you, I have added the lines: csvout = csv.writer(open("mydata.csv", "wb")) csvout.writerows((psd2D)), however, the output is not in one row in Excel cells, are there any simple ways to put array into a single row in .csv file? – KvasDub May 13 '13 at 20:42

1 Answers1

0

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)

Example

Numpy is probably the easiest way to go in my opinion. You could get fancy and use the np.flatten module. 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:

1,1,1,1,1,1,1,1,1, # there are 9 ones there, I promise! 3x3

Note there is a trailing comma. I was able to open this in excel no problem, voila!

enter image description here

Community
  • 1
  • 1
heisenBug
  • 865
  • 9
  • 19