patchwork
May 19th, 2010, 12:33 AM
I am new to Python (and programming in general), so bear with me. I am searching a file for a matching term, and would like to have the location of the matched term returned for future processing.
Here is the relevant portion of the code:
f = open(file_list[i], 'r')
for id_ in f:
# Remove unwanted whitespace and split line into fields
id_ = id_.strip()
fields = id_.split('\"')
fields = [fields[1] ,fields[3], fields[5]]
# Search within newly created list for matching ID number
if ( fields[0].count(new_id) ):
print 'fields[0]', fields[0]
print 'f.tell()', f.tell()
matches.append(f.tell())This is probably something simple that I am completely missing, but shouldn't the f.tell() in the above loop return the location of the match?
My output as written returns the matched item (in fields[0]), and the total size of the file--not the location where the match was found.
EDIT: It seems that f.tell() is returning the full file size anywhere I place it in the iteration. Is this because of the read ahead buffer the file iterator uses?
If so, how might I return the location of the matched term (perhaps manually reading the file line by line instead?)?
Here is the relevant portion of the code:
f = open(file_list[i], 'r')
for id_ in f:
# Remove unwanted whitespace and split line into fields
id_ = id_.strip()
fields = id_.split('\"')
fields = [fields[1] ,fields[3], fields[5]]
# Search within newly created list for matching ID number
if ( fields[0].count(new_id) ):
print 'fields[0]', fields[0]
print 'f.tell()', f.tell()
matches.append(f.tell())This is probably something simple that I am completely missing, but shouldn't the f.tell() in the above loop return the location of the match?
My output as written returns the matched item (in fields[0]), and the total size of the file--not the location where the match was found.
EDIT: It seems that f.tell() is returning the full file size anywhere I place it in the iteration. Is this because of the read ahead buffer the file iterator uses?
If so, how might I return the location of the matched term (perhaps manually reading the file line by line instead?)?