print line
prints a character because buf
is a string, and iterating over a string yields the characters of the string as 1-character strings.
When you say that reading line-by-line was slow, how did you implement the read? If you were using readlines(), that would explain the slowness (see http://stupidpythonideas.blogspot.com/2013/06/readlines-considered-silly.html).
Files are iterable over their lines, and Python will pick a buffer size when iterating, so this might suit your needs:
for line in f:
# do search stuff
If you want to specify the buffer size manually, you could also do this:
buf = f.readlines(buffersize)
while buf:
for line in buf:
# do search stuff
buf = f.readlines(buffersize)
Though, the first of the two is usually better.