0

I have a long text in python. It consists of 20 rows. Is there any way I can get the text up until a certain row?

Let's say that the text is called text_variable.

I would the like to be able to do something like

first_seven_rows = text_variable[:row 7]

print(first_seven_rows) should then return the first seven lines of the text.

Could this maybe be done with regex? How do I specify that I want regex to extract everything up until the seventh "\n"?

I hope you understand my question.

devanthol
  • 1
  • 2
  • Yes, if you've read in the file into `text_variable`, then `first_seven_rows = text_variable[:7]` will do the trick. – quamrana Mar 17 '20 at 10:24
  • Won't that only extract the first seven characters? I want to extract the first seven rows of the text. – devanthol Mar 17 '20 at 10:27
  • You should really ask another question, but be exact with what `text` you have. Give an example. Anyway, perhaps you want: `first_seven_rows = text_variable.split()[:7]` – quamrana Mar 17 '20 at 10:30

0 Answers0