What's wrong with these codes as failed to read the strings in Chinese? Is it because Chinese characters can't be read on Mac? Many thanks

Peter Otten __peter__ at web.de
Fri Nov 9 03:16:36 EST 2018


Cameron Simpson wrote:

> In Python 3 the loop is much cleaner:
> 
>   with open('namelist.txt', encoding='utf-8') as f:
>     for line in f:
>       line = line.strip()
>       print("line =", line)

In Python 2.7 you can use io.open():

import io 

with io.open('namelist.txt', encoding='utf-8') as f:
    for line in f:
        line = line.strip()
        print "line =", line

However, if you have the choice go with Python 3 which is where all 
improvements to the language and its libraries go.




More information about the Python-list mailing list