Advertisement
Guest User

index method

a guest
May 20th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. filename = 'test.txt'
  2. delimiter = '"'
  3.  
  4. # opening file for read
  5. with open(filename) as fileobject:
  6.     # read once, forget about memory insufficiency for sake of I\O
  7.     filedata = fileobject.read()
  8.  
  9.     quote_positions = [] # tuples of quote indices
  10.     QUOTESTART = None # explicit init
  11.     for index, letter in enumerate(filedata):
  12.         if letter == delimiter:
  13.             if QUOTESTART:
  14.                 QUOTEEND = index
  15.                 quote_positions.append((QUOTESTART, QUOTEEND))
  16.                 QUOTESTART = None
  17.             else:
  18.                 QUOTESTART = index + 1 # we dont need delimiter itself
  19.  
  20.     for position in quote_positions:
  21.         START = position[0]
  22.         END = position[1]
  23.         quote = filedata[START:END]
  24.         print(quote)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement