Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to get string in sci. notation to a number?

56 views
Skip to first unread message

1.156

unread,
Nov 29, 2011, 7:07:50 AM11/29/11
to
I've been able to get a text file with data into a list of strings
of the following form:

{{"4.998865e+000", "9.564453e-006"},
{"5.630450e+000", "9.194384e-006"},
{"6.113276e+000", "8.134170e-006"}}

After many hours I've really gotten nowhere except that it's most
likely done with ToExpression which works fine with simple decimal
numbers in string form. Must I really split up the strings, suck out the
exponents, and then multiply them out to be able to use these numbers in
a ListPlot?
Any hints would be appreciated.
--
Sent from my plain desktop PC.

A Retey

unread,
Nov 29, 2011, 7:41:47 AM11/29/11
to
this is somewhat hidden, but the functionality is in the import
routines. Depending on what your file originally looked like it might be
more elegant and efficient to import it's content directly to a list of
numbers, but this should also work once you have a list as the above:

{{"4.998865e+000", "9.564453e-006"}, {"5.630450e+000",
"9.194384e-006"}, {"6.113276e+000", "8.134170e-006"}} /.
s_String :> ImportString[s, "Table"]

hth,

albert

Oleksandr Rasputinov

unread,
Nov 29, 2011, 7:56:07 AM11/29/11
to
In place of "e", Mathematica uses "*^". When reading output from a Python
program into Mathematica for analysis, I have used StringReplace to
convert to valid Mathematica syntax followed by ToExpression; this can be
faster and more convenient than Import in certain cases. However, if you
are happy with your current approach and just want to finish the job, you
can use e.g.:

lst = {
{"4.998865e+000", "9.564453e-006"},
{"5.630450e+000", "9.194384e-006"},
{"6.113276e+000", "8.134170e-006"}
}

Map[Sequence@@ImportString[#, "List", "Numeric" -> True] &, lst, {2}]

{{4.99887, 9.56445*10^-6},
{5.63045, 9.19438*10^-6},
{6.11328, 8.13417*10^-6}}

1.156

unread,
Nov 30, 2011, 3:24:53 AM11/30/11
to
Many thanks to Albert Retey, Oleksandr Rasputinov, and Bob Hanlon for
their quick and useful help with my problem.

Oleksandr Rasputinov

unread,
Nov 30, 2011, 3:25:54 AM11/30/11
to
On Tue, 29 Nov 2011 12:56:07 -0000, Oleksandr Rasputinov
<oleksandr_...@hmamail.com> wrote:

>
> In place of "e", Mathematica uses "*^". When reading output from a Python
> program into Mathematica for analysis, I have used StringReplace to
> convert to valid Mathematica syntax followed by ToExpression; this can be
> faster and more convenient than Import in certain cases.

Actually that approach might be convenient here too, and definitely faster
than ImportString on each element for large tables of values...

ToExpression@Internal`Deflatten[
(* StringReplace doesn't like nested lists... *)
StringReplace[Flatten[lst], "e" -> "*^"],
Dimensions[lst]

Bob Hanlon

unread,
Nov 30, 2011, 3:31:31 AM11/30/11
to
data = {{"4.998865e+000", "9.564453e-006"}, {"5.630450e+000",
"9.194384e-006"}, {"6.113276e+000", "8.134170e-006"}};

data2 = Map[ToExpression[StringSplit[#, "e"]] &,
data, {2}] /.
{m_?NumericQ, e_} -> m*10^e

{{4.99887, 9.56445*10^-6}, {5.63045, 9.19438*10^-6}, {6.11328, 8.13417*10^-6}}

data3 = data /.
x_String :> ToExpression[StringSplit[x, "e"]] /.
{m_?NumericQ, e_} ->
m*10^e

{{4.99887, 9.56445*10^-6}, {5.63045, 9.19438*10^-6}, {6.11328, 8.13417*10^-6}}

Although you should be able to automatically convert the data on import

data4 = Import[Export["test.txt", data, "Table"], "Table"]

{{4.99887, 9.56445*10^-6}, {5.63045, 9.19438*10^-6}, {6.11328, 8.13417*10^-6}}

data2 == data3 == data4

True


Bob Hanlon


On Tue, Nov 29, 2011 at 7:04 AM, 1.156 <r...@piovere.com> wrote:
> I've been able to get a text file with data into a list of strings
> of the following form:
>
> {{"4.998865e+000", "9.564453e-006"},
> {"5.630450e+000", "9.194384e-006"},
> {"6.113276e+000", "8.134170e-006"}}
>
> After many hours I've really gotten nowhere except that it's most
> likely done with ToExpression which works fine with simple decimal
> numbers in string form. Must I really split up the strings, suck out the
> exponents, and then multiply them out to be able to use these numbers in
> a ListPlot?
> Any hints would be appreciated.

Chris Degnen

unread,
Dec 1, 2011, 7:26:57 AM12/1/11
to
A few more variations:-

data={
{"4.998865e+000","9.564453e-006"},
{"5.630450e+000","9.194384e-006"},
{"6.113276e+000","8.134170e-006"}};

a=ImportString[ExportString[data,"Table"]];

Export["data.dat",data];
b=Import["data.dat"];

c=ReadList["data.dat",{Number,Number}];

Export["data.csv",data];
d=Import["data.csv"]

a===b===c===d

Ralph Dratman

unread,
Dec 2, 2011, 7:27:27 AM12/2/11
to
But that is so easy with pattern replacement. More experienced people
will surely manage it without using StringReplace, but I get flummoxed
on which kind of Hold to use.

In[652]:= flpnumsStr =
ToString[{{"4.998865e+000", "9.564453e-006"}, {"5.630450e+000",
"9.194384e-006"}, {"6.113276e+000", "8.134170e-006"}}]

Out[652]= "{{4.998865e+000, 9.564453e-006}, {5.630450e+000,
9.194384e-006}, {6.113276e+000, 8.134170e-006}}"

In[653]:= ToExpression[StringReplace[flpnumsStr, "e" -> "*10^"]]

Out[653]= {{4.99887, 9.56445*10^-6}, {5.63045,
9.19438*10^-6}, {6.11328, 8.13417*10^-6}}

Ralph Dratman


On Thu, Dec 1, 2011 at 5:53 AM, Chris Degnen <deg...@cwgsy.net> wrote:
> A few more variations:-
>
> data={
> {"4.998865e+000","9.564453e-006"},
> {"5.630450e+000","9.194384e-006"},
> {"6.113276e+000","8.134170e-006"}};
>


> a=ImportString[ExportString[data,"Table"]];
>
> Export["data.dat",data];
> b=Import["data.dat"];
>
> c=ReadList["data.dat",{Number,Number}];
>
> Export["data.csv",data];
> d=Import["data.csv"]
>
> a===b===c===d
>
>
> On Nov 29, 12:07 pm, "1.156" <r...@piovere.com> wrote:

Bill Rowe

unread,
Dec 3, 2011, 5:09:32 AM12/3/11
to
On 12/2/11 at 7:21 AM, ralph....@gmail.com (Ralph Dratman) wrote:

>But that is so easy with pattern replacement. More experienced
>people will surely manage it without using StringReplace, but I get
>flummoxed on which kind of Hold to use.

>In[652]:= flpnumsStr =
>ToString[{{"4.998865e+000", "9.564453e-006"}, {"5.630450e+000",
>"9.194384e-006"}, {"6.113276e+000", "8.134170e-006"}}]

>Out[652]= "{{4.998865e+000, 9.564453e-006}, {5.630450e+000,
>9.194384e-006}, {6.113276e+000, 8.134170e-006}}"

>In[653]:= ToExpression[StringReplace[flpnumsStr, "e" -> "*10^"]]

>Out[653]= {{4.99887, 9.56445*10^-6}, {5.63045,
>9.19438*10^-6}, {6.11328, 8.13417*10^-6}}

I assume you really didn't manually enter the strings in the
form of Mathematica lists. So, let me start by putting the
output of your ToString above into something that likely
resembles the original data, i.e.,

In[5]:= p =
StringReplace[
StringTake[
ToString[{{"4.998865e+000", "9.564453e-006"}, {"5.630450e+000",
"9.194384e-006"}, {"6.113276e+000",
"8.134170e-006"}}], {3, -3}], "}, {" -> "\n"]

Out[5]= 4.998865e+000, 9.564453e-006
5.630450e+000, 9.194384e-006
6.113276e+000, 8.134170e-006

Now, these can be read in as numbers simply by using ImportString

In[6]:= ImportString[p, "CSV"]

Out[6]= {{4.998865, 9.564453*^-6}, {5.63045, 9.194384*^-6},
{6.113276, 8.13417*^-6}}


Oleksandr Rasputinov

unread,
Jan 9, 2012, 3:16:14 AM1/9/12
to
On Tue, 29 Nov 2011 11:48:54 -0000, Oleksandr Rasputinov
<oleksandr_...@hmamail.com> wrote:

> On Tue, 29 Nov 2011 12:07:50 -0000, 1.156 <r...@piovere.com> wrote:
>
> In place of "e", Mathematica uses "*^". When reading output from a
> Python program into Mathematica for analysis, I have used StringReplace
> to convert to valid Mathematica syntax followed by ToExpression; this
> can be faster and more convenient than Import in certain cases. However,
> if you are happy with your current approach and just want to finish the
> job, you can use e.g.:
>
> lst = {
> {"4.998865e+000", "9.564453e-006"},
> {"5.630450e+000", "9.194384e-006"},
> {"6.113276e+000", "8.134170e-006"}
> }
>
> Map[Sequence@@ImportString[#, "List", "Numeric" -> True] &, lst, {2}]
>
> {{4.99887, 9.56445*10^-6},
> {5.63045, 9.19438*10^-6},
> {6.11328, 8.13417*10^-6}}

Perhaps it is not worth reviving this old thread, but I noticed another
way to perform this task which may be of interest given the relatively
frequent requirement for importation of numbers in this format into
Mathematica:

Map[Internal`StringToDouble, lst, {2}]

Internal`StringToDouble seems to work in Mathematica 7 and 8. It exists in
version 5.2 but appears to do nothing. I didn't try version 6.

0 new messages