So I have a data file, whose contents look like this:
(-0.998722569699-0.000124681423362j) (-0.00620217196207+0.0501472284j) (-53.0662993987-543.342932199j) (1837.15+0j) (3.7656639408e-16+0j)
(-0.998722569699-0.000124681423362j) (-0.00620217196207+0.0501472284j) (-53.0662993987-543.342932199j) (1837.15+0j) (3.7656639408e-16+0j)
(-0.998722569699-0.000124681423362j) (-0.00620217196207+0.0501472284j) (-53.0662993987-543.342932199j) (1837.15+0j) (3.7656639408e-16+0j)
(-0.998722569699-0.000124681423362j) (-0.00620217196207+0.0501472284j) (-53.0662993987-543.342932199j) (1837.15+0j) (3.7656639408e-16+0j)
I have this saved as "~/Documents/pythonfile.dat". This works with the example given:
Map[ToExpression@StringReplace[#, "e" -> "*^"] &,
Import["~/Documents/pythonfile.dat", "Table"], 1] /. j -> I
(*
{{-0.998723 - 0.000124681 I, -0.00620217 + 0.0501472 I, -53.0663 -
543.343 I, 1837.15,
3.76566*10^-16}, {-0.998723 - 0.000124681 I, -0.00620217 +
0.0501472 I, -53.0663 - 543.343 I, 1837.15,
3.76566*10^-16}, {-0.998723 - 0.000124681 I, -0.00620217 +
0.0501472 I, -53.0663 - 543.343 I, 1837.15,
3.76566*10^-16}, {-0.998723 - 0.000124681 I, -0.00620217 +
0.0501472 I, -53.0663 - 543.343 I, 1837.15, 3.76566*10^-16}}
*)
The main points are
- First import it as a "Table", this will put every complex number inside a table as a string.
- Next you need to process the strings. You could just use
ToExpression
to turn the string "(1+2j)" into the expression 1+2j
, at which point you replace the variable j
with the imaginary unit I
- But there is the added problem of the "e" notation for base ten. This wouldn't be a problem on its own, but in combination with the imaginary unit and string recognition, we have to adjust a bit, and replace "e" with "*^" before converting the strings to expressions.