-1

Given a text file, how would one find the line number corresponding to given start and end indices for a substring? Even though the value of any given span can be found, the data set I'm working with contains duplicates.

Steele Farnsworth
  • 863
  • 1
  • 6
  • 15
  • Can you please be more specific, and offer a sample input and what you expect to see. Also, please explain what you have done so far. – ajon Nov 27 '18 at 00:47
  • I have a text file that contains the phrase "Amphotericin B" between characters 1673 and 1687. I need a function that, given the text of the file and the start and end indices, returns the line number of that exact instance of the phrase. – Steele Farnsworth Nov 27 '18 at 00:54

1 Answers1

0

line_number = my_string[:start_index].count("\n")

I guess ...

my_string = """
hello
test
line 
numbers
"""

print(my_string[:14].count("\n")) # the 14th character is the 3rd line
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179