Can't I just access the specific line without loading the file to memory?
Yes use linecache from standard library.
'''--> l.txt
line 1.
line 2.
line 3.
'''
from linecache import getline
#Notice linecache count from 1
print getline('l.txt', 2).strip()
#--> line 2.
snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
for line in open('my.txt'):
print line,
This is memory efficient and fast way,it reads line by line.
Not the hole file into memory.
Am not sure(dont think so)it`s a way to get one line,without reading something into memory.
snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
I think you must read the files ones and generate index for seek positions in the file for each file and save the index. Do use snippsat's suggested way of reading file, not readlines. Once you have index finding the line is instantanous.
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
Did you try to generate next line with next?
print next(tx)
Binary search style of activity is usually handled by using the module bisect .
Very nice idea to use the islice to go specific line, but I do not understand why to write it so complicated?
>>> import itertools
>>> f = open('LICENSE.txt')
>>> print next(itertools.islice(f,10,11)),
in Reston, Virginia where he released several versions of the
>>> print next(f),
software.
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
I meant the way the function worked, I gave example of going to tenth line and reading eleventh after in my post directly using islice.
I finally read your first posts' code and tried to understand what was going on there. What I came up was with the data in your last post:
import os
gpath = r"d:\##DB\Google-4gram"
str_1st_letters ='U'
open(os.path.join(gpath, (r"4gm-00%s" % ((ord(str_1st_letters.upper())-ord('A'))*2 + 34)) ), 'w').write("""USB Drive SanDisk Cruzer 1072
USB Drive Shuttle , 61
USB Drive Shuttle / 43
USB Drive Shuttle </S> 46
USB Drive So Far 62
USB Drive Spec Sheet 53
USB Drive Specs : 81
USB Drive Speeds Up 42
USB Drive Starter Kit 330
USB Drive Stick 256 46
""")
strSearch = 'Shuttle'
#read a 5gram file match with first letter of input
with open(os.path.join(gpath, (r"4gm-00%s" % ((ord(str_1st_letters.upper())-ord('A'))*2 + 34)) )) as tx:
for nRange, str4gram in enumerate(str4gram.rstrip() for str4gram in tx if strSearch in str4gram):
print "%r found in %r at %ith line" % (strSearch, str4gram, nRange+1)
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852