Created
October 5, 2015 22:31
-
-
Save Visgean/49278f2b87839af9a38c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
def get_block_range (filename, lookupValue): | |
""" | |
Returns the index of the first line that contain ``lookupValue`` | |
and end of the file | |
# here the value was at line 941 and the file had 1000 lines. | |
>>> get_block_range('example.txt', 'example') | |
(941, 1000) | |
:rtype: tuple | |
""" | |
if not os.path.getsize(filename): | |
return None, 0 | |
with open(filename, 'r') as file: | |
lookup_position = None | |
for num, line in enumerate(file, 1): | |
if lookup_position is None and lookupValue in line: | |
lookup_position = num | |
return lookup_position, num |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
asx | |
asx | |
as | |
x | |
asxfsdvsf | |
gbs | |
bs | |
dfv | |
sdfvsdf | |
bsb | |
dfv | |
example | |
gsf | |
b | |
fg | |
bsf | |
gb | |
sfgb | |
sfg | |
b | |
sfgb | |
sf | |
bg | |
sfg | |
b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [10]: asx.get_block_range('example.txt', 'example') | |
Out[10]: (12, 27) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment