# read Python-generated data file

I've read similar-but-not-quite-the-same questions : I have to read a python-generated data file which contains complex numbers. So, the numbers appear as (real+imag j).

And of course Mathematica read them as strings using

Import["","Table"]


I've tried fiddling with StringSplit or Read[StringToStream,] to no avail...

Any help would be much appreciated.

• Paste a little snippet of the data file so we can see what the format is exactly. – Jason B. Feb 10 '16 at 10:04
• If you are exporting the data from Python yourself, the best solution will be to split the complex numbers into real and imaginary parts and export them as distinct units. Parsing special forms in Mathematica is possible but not fast. If your data file is a simple table with only real numbers, there are very fast builtin functions to read it. – Szabolcs Feb 10 '16 at 10:06
• Unfortunately, I am not the one generating the data file. Here are the first few elements : (1e-07+0j) (-0.998722569699-0.000124681423362j) (-0.00620217196207+0.0501472284707j) (-53.0662993987-543.342932199j) (1837.15+0j) (3.7656639408e-16+0j) – coussin Feb 10 '16 at 10:18
• @coussin, is the file like that, all on one line? Or is each complex number on a separate line, with parantheses? – Jason B. Feb 10 '16 at 10:44
• @coussin please edit your question to add any extra information, such as data examples. It would be a good idea to offer a permanent paste on pastebin.com so we can see the format of the line and fields separators and headers if present at all. See this question on meta for ways for sharing data. Also please try to share code in formatted form. – rhermans Feb 10 '16 at 10:56

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

1. First import it as a "Table", this will put every complex number inside a table as a string.
2. 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
3. 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.
• Thanks Jason B, that works ! Thanks everyone :) – coussin Feb 10 '16 at 13:14