3
$\begingroup$

I want to download the content of the website(contains text) and only a few lines from the content (from a specific number of the line up to the last line minus specific offset). Unfortunately, I do not know how to get the number of line in the content. For example, I want to replace 59 with the length of the content minus specific offset.

data1 = Import["https://en.wikipedia.org/wiki/Segment_tree"];
Snippet[data1, 51 ;; 59]
$\endgroup$

closed as unclear what you're asking by corey979, MarcoB, Carl Lange, happy fish, bbgodfrey Apr 28 at 15:48

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ You mean from line 51 to the end? Snippet[data1, 51 ;;]. $\endgroup$ – corey979 Apr 23 at 12:26
  • $\begingroup$ @corey979 I edit the question to be more specific $\endgroup$ – Kiril Danilchenko Apr 23 at 12:33
  • 1
    $\begingroup$ Snippet[data1, 51 ;; -17] $\endgroup$ – corey979 Apr 23 at 12:38
  • $\begingroup$ @corey979 thank you for the answer,but if I'm interested to know the length of the content. How I can get it? $\endgroup$ – Kiril Danilchenko Apr 23 at 12:46
  • 1
    $\begingroup$ So you want to know the length of the content, not take its snippet from a given line to the end, or to some numbered line from the end? So that's quite different than you have in the question: "replace 59 with the length of the content minus specific offset" would give the same outputs as my codes above. $\endgroup$ – corey979 Apr 23 at 14:07
4
$\begingroup$

One approach is to split the imported data on newlines using StringSplit so the length of the resulting list is the line count. Then we can use Riffle and StringJoin to get the selected lines:

Module[{start = 51, offset = 85, raw, data, linecount, stop},
 raw = Import["https://en.wikipedia.org/wiki/Segment_tree"];
 data = StringTrim@StringSplit[raw, "\n"];
 linecount = Length@data;
 stop = linecount - offset;
 Print["# lines: ", linecount];
 Print["  start: ", start];
 Print["   stop: ", stop];
 Print["# saved: ", stop - start];
 data = Take[data, {start, stop}];
 Riffle[data, " "] // StringJoin]

Alternatively, we could count newlines in the raw data and use Snippet as corey979 points out in the comments:

 Module[{start = 51, offset = 85, raw, linecount, stop, counter = 0},
 raw = Import["https://en.wikipedia.org/wiki/Segment_tree"];
 linecount = Block[{str = StringToStream[raw]},
   While[Read[str, Record, NullRecords -> True] =!= EndOfFile, 
    counter++];
   Close[str]; counter];
 stop = linecount - offset;
 Print["# lines: ", linecount];
 Print["  start: ", start];
 Print["   stop: ", stop];
 Print["# saved: ", stop - start];
 Snippet[raw, start ;; stop]]

The result for the website specified in the question gives:

This section describes the query operation of a segment tree in a \ one-dimensional space. A query for a segment tree, receives a point q \ x (should be one of the leaves of tree), and retrieves a list of all \ the segments stored which contain the point q x . Formally stated; \ given a node (subtree) v and a query point q x , the query can be \ done using the following algorithm: Report all the intervals in I ( v \ ). If v is not a leaf: If q x is in Int(left child of v ) then

$\endgroup$
5
$\begingroup$
length = ToExpression[ToString[StringCount[#,"\n"] & /@ FullForm[data1]]]+1
Snippet[data1,138;;length]

This page was last edited on 27 January 2019, at 05:26 (UTC) . Text \ is available under the Creative Commons Attribution-ShareAlike \ License ; additional terms may apply. By using this site, you agree \ to the Terms of Use and Privacy Policy . Wikipedia® is a registered \ trademark of the Wikimedia Foundation, Inc. , a non-profit \ organization.
Privacy policy About Wikipedia Disclaimers Contact Wikipedia \ Developers Cookie statement Mobile view

$\endgroup$

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