Hi all,

I am trying to write a list of complex numbers to a csv file. The list looks like:

cpl1 = [(-6.4703169037894082e-12-2.7429131015110908e-12j), (-9.0688415110840026e-11+1.7513018812920222e-11j), (-1.8522787470498514e-10+1.3074128585310874e-11j), (-5.8563154325952382e-10-6.4430377699764563e-10j), (2.1451734832922398e-10-9.987741744765799e-10j), (2.9095870157647141e-10-6.3227384439201728e-10j), (1.9448214150230569e-09+5.1612969453884716e-10j)]

The complex numbers are to be written to file to look like:

real, imaginary, real, imaginary, real, imaginary, ...etc

To do this I would use the following code
(PLEASE NOTE THIS IS ONLY A SUMMARY OF A VERY LONG CODE.
IT IS ONLY MEANT AS A GUIDE.):

#information taken from external file
f = csv.writer(open("cplx.csv", "wb"))
cpl1 = []

    #CODE THAT CREATES cpl1 using info from external file. It is far too long to post here.

f.writerow([str(np.real(cpl1[0])),str(np.imag(cpl1[0])),str(np.real(cpl1[1])),str(np.imag(cpl1[1])),str(np.real(cpl1[2])),str(np.imag(cpl1[2])),str(np.real(cpl1[3])),str(np.imag(cpl1[3])),str(np.real(cpl1[4])),str(np.imag(cpl1[4])),str(np.real(cpl1[5])),str(np.imag(cpl1[5])),str(np.real(cpl1[6])),str(np.imag(cpl1[6])),str(np.real(cpl1[7])),str(np.imag(cpl1[7]))])
#After writing the code loops

This works fine for subsequent complex numbers of a constant length.
However in my present case the subsequently generated complex numbers are of different lengths (this is set by an external file).
Taking into account the variability of the subsequent length of cpl1 is giving me the most problems.

Any ideas will be appreciated.

cheers,
h

Here is one simple solution, but it puts comma after the last value also, if that is problem you should print all but last in the loop and print separately the last one. There is also more advanced methods of formatting, but looking the style of your code thought this most understandable for you:

cpl1 = [(-6.4703169037894082e-12-2.7429131015110908e-12j), (-9.0688415110840026e-11+1.7513018812920222e-11j), (-1.8522787470498514e-10+1.3074128585310874e-11j), (-5.8563154325952382e-10-6.4430377699764563e-10j), (2.1451734832922398e-10-9.987741744765799e-10j), (2.9095870157647141e-10-6.3227384439201728e-10j), (1.9448214150230569e-09+5.1612969453884716e-10j)]
with open('cplx.csv', 'w') as f:
    for c in cpl1:
        f.write(str(c.real) + ', ' + str(c.imag) + ', ')
    f.write('\n')

Thanks a lot pyTony ... this solved my problem.

cheers,
h.

Here is one simple solution, but it puts comma after the last value also, if that is problem you should print all but last in the loop and print separately the last one. There is also more advanced methods of formatting, but looking the style of your code thought this most understandable for you:

cpl1 = [(-6.4703169037894082e-12-2.7429131015110908e-12j), (-9.0688415110840026e-11+1.7513018812920222e-11j), (-1.8522787470498514e-10+1.3074128585310874e-11j), (-5.8563154325952382e-10-6.4430377699764563e-10j), (2.1451734832922398e-10-9.987741744765799e-10j), (2.9095870157647141e-10-6.3227384439201728e-10j), (1.9448214150230569e-09+5.1612969453884716e-10j)]
with open('cplx.csv', 'w') as f:
    for c in cpl1:
        f.write(str(c.real) + ', ' + str(c.imag) + ', ')
    f.write('\n')

I know this thread was marked "solved", but I think the following is a somewhat simpler solution:

f.write(",".join(["%s,%s" % (item.real, item.imag) for item in cpl1]))
This question has already been answered. Start a new discussion instead.