Quantcast
Channel: Active questions tagged string-manipulation - Mathematica Stack Exchange
Viewing all articles
Browse latest Browse all 186

Converting a string containing whitespace-delimited numbers to a list of numbers

$
0
0

Suppose I have a string str that contains only whitespace-delimited real numbers:

str = " 0\t1.46604\t1.44829\t12.0546\t1.57075\t1.64044\t12.0489\t1.58142";

I would like to convert str to a list of real numbers. One way to accomplish this is to split the string using StringSplit and then Map the function ToExpression across the resulting list:

Map[ToExpression, StringSplit[str]]

which gives the correct output:

{0, 1.46604, 1.44829, 12.0546, 1.57075, 1.64044, 12.0489, 1.58142}

But, is there a cleaner or more efficient way to do this? I will be converting about 100,000 strings str -- each of which are approximately the same length as the example str above -- which will be read as records from a text file (i.e., using OpenRead and Read), so it would be nice if the method is relatively efficient. Thank you for your time!


Viewing all articles
Browse latest Browse all 186

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>