4

I'm trying to write code which imports and exports lists of complex numbers in Python. So far I'm attempting this using the csv module. I've exported the data to a file using:

spamWriter = csv.writer(open('data.csv', 'wb')
spamWriter.writerow(complex_data)

Where complex data is a list numbers generated by the complex(re,im) function. Ex:

print complex_data
[(37470-880j),(35093-791j),(33920-981j),(28579-789j),(48002-574j),(46607-2317j),(42353-1557j),(45166-2520j),(45594-232j),(41149+561j)]

To then import this at a later time, I try the following:

mycsv = csv.reader(open('data.csv', 'rb'))
out = list(mycsv)
print out
[['(37470-880j)','(35093-791j)','(33920-981j)','(28579-789j)','(48002-574j)','(46607-2317j)','(42353-1557j)','(45166-2520j)','(45594-232j)','(41149+561j)']]

(Note that this is a list of lists, I just happened to use only one row for the example.)

I now need to turn this into complex numbers rather than strings. I think there should be a way to do this with mapping as in this question, but I couldn't figure out how to make it work. Any help would be appreciated!

Alternatively, if there's any easier way to import/export complex-valued data that I don't know of, I'd be happy to try something else entirely.

Community
  • 1
  • 1
new_sysadmin
  • 165
  • 1
  • 6

5 Answers5

9

Just pass the string to complex():

>>> complex('(37470-880j)')
(37470-880j)

Like int() it takes a string representation of a complex number and parses that. You can use map() to do so for a list:

map(complex, row)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
8
>>> c = ['(37470-880j)','(35093-791j)','(33920-981j)']
>>> map(complex, c)
[(37470-880j), (35093-791j), (33920-981j)]
applicative_functor
  • 4,926
  • 2
  • 23
  • 34
  • @Martijn Pieters beat you by 3 minutes, but +1 nonetheless – inspectorG4dget Jul 25 '12 at 22:06
  • This does not work in python 3. Map has been (mostly) deprecated: https://stackoverflow.com/questions/40015439/why-does-map-return-a-map-object-instead-of-a-list-in-python-3 – Max Mar 23 '20 at 21:31
1
complex_out = []
for row in out:
    comp_row = [complex(x) for x in row]
    complex_out.append(comp_row)
Guy Adini
  • 5,188
  • 5
  • 32
  • 34
0

CSV docs say:

Note that complex numbers are written out surrounded by parens. This may cause some problems for other programs which read CSV files (assuming they support complex numbers at all).

This should convert elements in 'out' to complex numbers from the string types, which is the simplest solution given your existing code with ease of handling non-complex entries.

 for i,row in enumerate(out):
      j,entry in enumerate(row):
          try:
               out[i][j] = complex(out[i][entry])
          except ValueError:
               # Print here if you want to know something bad happened
               pass

Otherwise using map(complex, row) on each row takes fewer lines.

 for i,row in enumerate(out):
      out[i] = map(complex, row)
Pyrce
  • 8,296
  • 3
  • 31
  • 46
0

I think each method above is bit complex

Easiest Way is this

In [1]: complex_num = '-2+3j'

In [2]: complex(complex_num)
Out[2]: (-2+3j)
Anurag Misra
  • 1,516
  • 18
  • 24