3
$\begingroup$

I have a file let's say 1.txt having numbers like:

fgdgfdg     fhdfdh
0.000105533 1.00E-04
0.222281    1.00E-04
0.444456    1.00E-04
0.666631    1.00E-04
asdas       ddfdsfsd
0.111193    0.166733
0.333369    0.166733
sadsd       dfffsas
0.777719    0.166733

I want to delete all this unknown string during the import process. It would be great help if you can provide me some solutions. I have tried various commands like delete, replace, cases but it doesnt work .... thanks

$\endgroup$
  • $\begingroup$ What exactly do you want to delete? all strings in the txt file while importing all numbers? $\endgroup$ – g3kk0 Jul 28 '13 at 21:10
7
$\begingroup$

As g3kk0 showed Mathematica can easily read this data with Import.

I saved the data to a file named TableWithText.txt and placed it in the path given by Directory[] (which itself can be set with SetDirectory.)

data = Import["TableWithText.txt", "Table"]
{{"fgdgfdg", "fhdfdh"}, {0.000105533, 0.0001}, {0.222281, 0.0001}, {0.444456, 
  0.0001}, {0.666631, 0.0001}, {"asdas", "ddfdsfsd"}, {0.111193, 0.166733}, {0.333369, 
  0.166733}, {"sadsd", "dfffsas"}, {0.777719, 0.166733}}

From there you can strip the individual Strings with:

DeleteCases[data, _String, {-1}]
{{}, {0.000105533, 0.0001}, {0.222281, 0.0001}, {0.444456, 0.0001}, {0.666631, 
  0.0001}, {}, {0.111193, 0.166733}, {0.333369, 0.166733}, {}, {0.777719, 0.166733}}

Or entire rows that are composed solely of Strings:

DeleteCases[data, {__String}, {-2}]
{{0.000105533, 0.0001}, {0.222281, 0.0001}, {0.444456, 0.0001}, {0.666631, 
  0.0001}, {0.111193, 0.166733}, {0.333369, 0.166733}, {0.777719, 0.166733}}
| improve this answer | |
$\endgroup$
5
$\begingroup$

Assuming your .txt file looks like the following:

enter image description here

you can use the Import function of Mathematica with the option "Data" to import your data into the list representation:

path="...\\..."; (*path to .txt file*)
data=Import[path, "Data"]
 {{"fgdgfdg", "fhdfdh"}, {0.000105533, 0.0001}, {0.222281, 
 0.0001}, {0.444456, 0.0001}, {0.666631, 0.0001}, {"asdas", 
 "ddfdsfsd"}, {0.111193, 0.166733}, {0.333369, 0.166733}, {"sadsd", 
 "dfffsas"}, {0.777719, 0.166733}}

Now you can select for specific entries. Assuming that you want to keep all entries in a one-dimensional list, I first flatten the data (Flatten) and then use the Select function to select for elements that are numeric:

Select[Flatten[data], NumberQ[#] &]
{0.000105533, 0.0001, 0.222281, 0.0001, 0.444456, 0.0001, 0.666631,
0.0001, 0.111193, 0.166733, 0.333369, 0.166733, 0.777719, 0.166733}

Or using Cases:

Cases[Flatten@data, _Real]
{0.000105533, 0.0001, 0.222281, 0.0001, 0.444456, 0.0001, 0.666631,
0.0001, 0.111193, 0.166733, 0.333369, 0.166733, 0.777719, 0.166733}
| improve this answer | |
$\endgroup$

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.