read Unicode characters one by one in python2

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Feb 25 11:57:58 EST 2018


On Mon, 26 Feb 2018 01:50:16 +1100, Chris Angelico wrote:

> If you actually need character-by-character, you'd need "for character
> in fh.read()" rather than iterating over the file itself. Iterating over
> a file yields lines.

Indeed. But I wonder if there's a performance cost/gain to iterating over 
each line, rather than reading one char at a time?

for line in file:
    for c in line:
        ...

Too lazy to actually test it myself, but just tossing this idea out in 
case anyone else cares to give it a try.


-- 
Steve




More information about the Python-list mailing list