1
$\begingroup$

I'd like to extract the "te band" values from this list:

"tefreqs:", "k index", "k1", "k2", "k3", "kmag/2pi", "te band 1", "te \
band 2", "te band 3", "te band 4", "te band 5", "te band 6", "te band \
7", "te band 8", "tefreqs:", 1, 0, 0, 0, 0, 0, 0.561229, 0.793929, \
0.794659, 1.03341, 1.03342, 1.11597, 1.11681, "tefreqs:", 2, 0, 0.1, \
0, 0.11547, 0.101865, 0.559805, 0.79146, 0.793258, 0.992126, 1.02845, \
1.09799, 1.1151,

Apologies for the formatting of the list, I'm sure there's a nicer way to show it.

I'm guessing this might involve the Take command in some way - I'd like to extract the 7th-14th values, then the 21-28th values etc. I wanted to try using Take to say take eight values starting on the 7th element, then leave six, then take the next eight, but in the documentation I can't see that Mathematica allows extraction of a range.

$\endgroup$
  • 1
    $\begingroup$ Does this come from importing a file? I suspect Import[] with the appropriate settings would be a much cleaner job than what you're trying to do. $\endgroup$ – J. M.'s technical difficulties Nov 9 '17 at 17:40
  • $\begingroup$ @J.M I've had trouble with that. If I import as data then yes I can extract the columns I'd like, but Mathematica imports everything as strings and the ToExpression command tells me the syntax is invalid so I can't then convert it to numbers. Importing as CSV means Mathematica recognises them as numbers. $\endgroup$ – user13948 Nov 9 '17 at 17:42
  • $\begingroup$ Would you mind posting the file you have, on Pastebin perhaps? $\endgroup$ – J. M.'s technical difficulties Nov 9 '17 at 17:43
  • 2
    $\begingroup$ This works: Drop[Import["https://pastebin.com/raw/SybuQE6G", "CSV", "HeaderLines" -> 1], None, 7]. In your case, replace the URL with the location of your file. $\endgroup$ – J. M.'s technical difficulties Nov 9 '17 at 17:52
  • 1
    $\begingroup$ In the future, you might want to give some context on any issue you encounter, so that people are not pursuing XY problems. $\endgroup$ – J. M.'s technical difficulties Nov 9 '17 at 18:02
2
$\begingroup$

I think you could try with this.

I have edited your text, to check whether it works with numbers having more than one digit:

text = {"tefreqs:", "k index", "k1", "k2", "k3", "kmag/2pi", 
"te band 1", "te band 2", "te band 3", "te band 4", "te band 5", 
"te band 6", "te band 7", "te band 8", "te band 45", "te band 201", 
"tefreqs:", 1, 0, 0, 0, 0, 0, 0.561229, 0.793929, 0.794659, 1.03341,
1.03342, 1.11597, 1.11681, "tefreqs:", 2, 0, 0.1, 0, 0.11547, 
0.101865, 0.559805, 0.79146, 0.793258, 0.992126, 1.02845, 1.09799, 
1.1151};

str = Flatten@(If[! NumericQ[#],If[StringMatchQ[#, "te band" ~~ __], {#}, Nothing], Nothing] & /@ text);

ToExpression@StringJoin[#] & /@ Map[If[DigitQ[#], #, Nothing] &, (Characters[#] & /@ str), {2}]

(* Out[143]:= {1, 2, 3, 4, 5, 6, 7, 8, 45, 201} *)
|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.