osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


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.