Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #6805 > unrolled thread
| Started by | "1.156" <rob@piovere.com> |
|---|---|
| First post | 2011-11-29 12:07 +0000 |
| Last post | 2012-01-09 08:16 +0000 |
| Articles | 7 — 5 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
how to get string in sci. notation to a number? "1.156" <rob@piovere.com> - 2011-11-29 12:07 +0000
Re: how to get string in sci. notation to a number? A Retey <awnl@gmx-topmail.de> - 2011-11-29 12:41 +0000
Re: how to get string in sci. notation to a number? "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-11-29 12:56 +0000
Re: how to get string in sci. notation to a number? "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-11-30 08:25 +0000
Re: how to get string in sci. notation to a number? Chris Degnen <degnen@cwgsy.net> - 2011-12-01 12:26 +0000
Re: how to get string in sci. notation to a number? Ralph Dratman <ralph.dratman@gmail.com> - 2011-12-02 12:27 +0000
Re: how to get string in sci. notation to a number? "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2012-01-09 08:16 +0000
| From | "1.156" <rob@piovere.com> |
|---|---|
| Date | 2011-11-29 12:07 +0000 |
| Subject | how to get string in sci. notation to a number? |
| Message-ID | <jb2hum$5e0$1@smc.vnet.net> |
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.
[toc] | [next] | [standalone]
| From | A Retey <awnl@gmx-topmail.de> |
|---|---|
| Date | 2011-11-29 12:41 +0000 |
| Message-ID | <jb2jub$688$1@smc.vnet.net> |
| In reply to | #6805 |
Am 29.11.2011 13:07, schrieb 1.156:
> 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.
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
[toc] | [prev] | [next] | [standalone]
| From | "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> |
|---|---|
| Date | 2011-11-29 12:56 +0000 |
| Message-ID | <jb2kp7$6j4$1@smc.vnet.net> |
| In reply to | #6805 |
On Tue, 29 Nov 2011 12:07:50 -0000, 1.156 <rob@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.
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}}
[toc] | [prev] | [next] | [standalone]
| From | "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> |
|---|---|
| Date | 2011-11-30 08:25 +0000 |
| Message-ID | <jb4pai$gsh$1@smc.vnet.net> |
| In reply to | #6821 |
On Tue, 29 Nov 2011 12:56:07 -0000, Oleksandr Rasputinov
<oleksandr_rasputinov@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]
]
{{4.99887, 9.56445*10^-6},
{5.63045, 9.19438*10^-6},
{6.11328, 8.13417*10^-6}}
[toc] | [prev] | [next] | [standalone]
| From | Chris Degnen <degnen@cwgsy.net> |
|---|---|
| Date | 2011-12-01 12:26 +0000 |
| Message-ID | <jb7rqh$j2s$1@smc.vnet.net> |
| In reply to | #6805 |
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:
> 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.
[toc] | [prev] | [next] | [standalone]
| From | Ralph Dratman <ralph.dratman@gmail.com> |
|---|---|
| Date | 2011-12-02 12:27 +0000 |
| Message-ID | <jbag7f$212$1@smc.vnet.net> |
| In reply to | #6805 |
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 <degnen@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:
>> 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.
>
>
[toc] | [prev] | [next] | [standalone]
| From | "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> |
|---|---|
| Date | 2012-01-09 08:16 +0000 |
| Message-ID | <jee7oe$cje$1@smc.vnet.net> |
| In reply to | #6805 |
On Tue, 29 Nov 2011 11:48:54 -0000, Oleksandr Rasputinov
<oleksandr_rasputinov@hmamail.com> wrote:
> On Tue, 29 Nov 2011 12:07:50 -0000, 1.156 <rob@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.
>
> 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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web