0
$\begingroup$

I've the following code:

Import["test.txt"]

And it gives me:

1,10

2,20

3,30

4,40

5,50

6,60

7,70

8,80

9,90

10,100

11,110

12,120

13,130

14,140

15,150

16,160

17,170

18,180

19,190

But I only want the time values $8,9,10$ how can I improve my code so that only those times are printed in my notebook?


I used this code but that does not work, but why?

s = Import["test.txt"];
data = ToExpression@StringSplit[
    Rest[
     StringSplit[s, {""}]
     ],
    ","];
Select[data, MemberQ[{8, 9, 10}, #[[1]]] &]
$\endgroup$
1
$\begingroup$

The following seems to work:

data = Table[{i, i*10}, {i, 1, 19}];

which mimics what final form of the data should look like

Select[data, MemberQ[{8, 9, 10}, #[[1]]] &]
(*{{8, 80}, {9, 90}, {10, 100}}*)

I am not certain if the contents of your test file are as you posted or there are just typos. But using ImportString[] to sit in for Import[] for immediate results

    data2 = ImportString["1,10
2,20
3,30
4,40
5,50
6,60
7,70
8,80
9,90
10,100
11,110 
12,120
13,130
14,140
15,150
16,160
17,170
18,180
19,190", "CSV"];
Select[data2, MemberQ[{8, 9, 10}, #[[1]]] &]
(*{{8, 80}, {9, 90}, {10, 100}}*)

So Try

data = Import["test.txt", "CSV"]
$\endgroup$
  • $\begingroup$ Yes, I was just about to suggest the Import["test.txt", "CSV"] command $\endgroup$ – KraZug May 4 '18 at 8:44
  • $\begingroup$ @KraZug The problem with this is, that the .txt file in reality much bigger is. So I've soooo much measured points that I want to stick with my code but I think there is problem in my code in the data = ToExpression@ StringSplit[Rest[StringSplit[s, {"(", ")(", ")"}]], ","] part. $\endgroup$ – Looper May 4 '18 at 8:45
  • $\begingroup$ @Hans See my reaction to KraZug. $\endgroup$ – Looper May 4 '18 at 8:45
  • $\begingroup$ @Looper, you mean that you have lots of other bits of text in the .txt file? $\endgroup$ – KraZug May 4 '18 at 8:48
  • $\begingroup$ @KraZug Yes indeed, I asked the question here with a lower amount of points because if my code works it also works with a lot of data point. $\endgroup$ – Looper May 4 '18 at 8:49
0
$\begingroup$

If you take the code you have given and evaluate it one part at a time, starting from the inside, and you'll find it doesn't do what you want it to (it starts by splits on every character and then drops the first character for some reason).

How about this function:

selectNumberLines[textData_, num_] :=  Pick[StringSplit[textData, "\n"], 
StringContainsQ[StringSplit[textData, "\n"], StartOfLine ~~ ToString[num] ~~ "," ~~ ___]]

It splits the block of text from the Input into individual lines before finding those that start with the number that you give.

selectNumberLines[s, #] & /@ {8, 9, 10}
 (* {{"8,80"}, {"9,90"}, {"10,100"}}  *)

If your data is always of the form "number, stuff you want", you could try this to get only the later parts:

selectNumberLinesValues[textData_, num_] := 
 ToExpression@Last@Last@StringSplit[selectNumberLines[textData,num], ","]

selectNumberLinesValues[s, #] & /@ {8, 9, 10}
(* {80, 90, 100} *)

But you need to be careful with matching patterns like this.

$\endgroup$
  • $\begingroup$ The problem with this is, that I only want to select on the first number (that is the time) so when I want to extract $8,9$ and $10$ I only knows those times and not the $80,90$ and $100$ part. $\endgroup$ – Looper May 4 '18 at 9:13
  • $\begingroup$ @Looper, that code only takes the first value and returns the second part. $\endgroup$ – KraZug May 4 '18 at 12:33

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.