0

What I’m looking for is take a specific chunk or segment from text file. How can I indicate to Python where to start and finish the reading or printing? I found this example code but I couldn’t find an explanation for this particular technique. I know how to use the method “.split()” with strings but there are additional parameters that I don’t understand.

a = open("text_1.txt", "r")

text = a.read()

print (text.split("<--")[1].split("-->")[1])

# Here this code splits the text on the text file, starting from
# the value inside of the second ".split()" and ends in the value
# inside the first. What I don't understand what means the one in brackets `[1].`

a.close()

As you may already notice, in this case, the text I want to print is between "--> ..... and ..... <--" symbols.

In addition, What if there are not symbols to delimit the text?, What if I would like to isolate a whole paragraph, without lossing the first and last words? What if there is more than one word or symbol that is specified inside the ".split( )" method?

Is there any way to indicate Python that I want to select lines according its line-number (in the case of importing a web page with urllib)?

Thanks in advance

Ariel
  • 1
  • 2

2 Answers2

1

When python reads a file it converts it into a list. Split is a string method. One way of doing this would be to convert it into a string and then split it like you were trying to do and write that into a file. Check dir(list) and dir(str) for methods.

Andrew Mayes
  • 95
  • 10
0

To answer one of your several questions, [1] pulls the 1th item (zero-based) from the “primary” to the left. In Python, it's called a subscript.

In your case, text.split(...) returns a list of tokens, and the subsequent [1] selects just one of them. Here's what's happening:

>>> 'foo bar baz'.split()
['foo', 'bar', 'baz']

>>> 'foo bar baz'.split()[1]
'bar'

If you haven't encountered this syntax before, you may want to work through a Python tutorial before continuing with your problem here.

Ben Graham
  • 2,089
  • 17
  • 21