0
$\begingroup$

The .txt file is:

tb_FeSe_22_12_2014.csv 0.00102786

(-0.000421863,1.58589e-09)(-4.04766e-05,2.67116e-07)(-8.4396e-06,-1.54976e-07)

I would like to Import it to Mathematica such that I have:

a=0.00102786;

b={-0.000421863+I*1.58589*10^-09,-4.04766*10^-05+I*2.67116*10^-07,-8.4396*10^-06+I*-1.54976*10^-07};

I tried to "Import", and then "StringReplace" to try to create an array, but I did not achieve anything. I think that part of the difficulty comes from the fact that the numbers in the .txt are not separated by comas.

What can I do?

$\endgroup$
1
$\begingroup$

Corrected with 2012rcampion's feedback

    data = Import["test.txt"]
    (*"(-0.000421863,1.58589e-09)(-4.04766e-05,2.67116e-07)(-8.4396e-06,-1.\
    54976e-07)
    (-0.000421863,1.58589e-09)(-4.04766e-05,2.67116e-07)(-8.4396e-06,-1.\
    54976e-07)"*)

    data = ToExpression[
     StringSplit[
      StringReplace[data, {","->"+I*","(" -> "", "e" -> "*^"}], {",", ")"}]]

(*{-0.000421863 + 1.58589*10^-9 I, -0.0000404766 + 
  2.67116*10^-7 I, -8.4396*10^-6 - 1.54976*10^-7 I, -0.000421863 + 
  1.58589*10^-9 I, -0.0000404766 + 2.67116*10^-7 I, -8.4396*10^-6 - 
  1.54976*10^-7 I}*)
$\endgroup$
  • $\begingroup$ Thanks @Zviovich , but the output is not the one I want (named as b in the post) $\endgroup$ – Mencia Mar 24 '15 at 17:41
  • $\begingroup$ Try StringSplit[StringReplace[data, {"," -> "+I*", "e" -> "*^"}], ("(" | ")") ..] $\endgroup$ – 2012rcampion Mar 24 '15 at 17:51
  • $\begingroup$ Thanks @2012rcampion, it creates an array, but Mathematica treats the elements as strings, not as numbers. How can I fix that? $\endgroup$ – Mencia Mar 24 '15 at 18:03
  • $\begingroup$ @mencia if you look at the answer that I updated with 2012rcampion feedback you can see that there is a ToExpression command which makes it a complex number. Check with Head[#]&/@data which returns {Complex, Complex, Complex, Complex, Complex, Complex} $\endgroup$ – Zviovich Mar 24 '15 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.