Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question
    
What exactly do you want to delete? all strings in the txt file while importing all numbers? –  g3kk0 Jul 28 '13 at 21:10

2 Answers 2

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}}
share|improve this answer

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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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