read Unicode characters one by one in python2

Chris Warrick kwpolska at gmail.com
Sun Feb 25 08:33:33 EST 2018


On 24 February 2018 at 17:17, Peng Yu <pengyu.ut at gmail.com> wrote:
> Here shows some code for reading Unicode characters one by one in
> python2. Is it the best code for reading Unicode characters one by one
> in python2?
>
> https://rosettacode.org/wiki/Read_a_file_character_by_character/UTF8#Python

No, it’s terrible. So is the Python 3 version. All you need for both
Pythons is this:

import io
with io.open('input.txt', 'r', encoding='utf-8') as fh:
    for character in fh:
        print(character)

(and please make sure you need to read character-by-character first)

-- 
Chris Warrick <https://chriswarrick.com/>
PGP: 5EAAEA16



More information about the Python-list mailing list