Re: 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

2018-11-09 Thread Kevin Hu
Dear Annie, Looks like you’re trying to read in a file with encoded characters. By default Python3 reads file in bytes, and you’ll need to decode it. Looks like it’s utf-8 encoding. More at: https://docs.python.org/3/howto/unicode.html You can

Re: 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

2018-11-09 Thread Peter Otten
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')

Re: 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

2018-11-08 Thread Cameron Simpson
On 08Nov2018 19:30, Annie Lu wrote: # -*- coding: UTF-8 -*- ... f = open('/Users/annielu/Desktop/namelist1801.txt') namelist1801txt = f.read() f.close() namelist1801txt

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

2018-11-08 Thread Annie Lu
# -*- coding: UTF-8 -*- ... f = open('/Users/annielu/Desktop/namelist1801.txt') >>> namelist1801txt = f.read() >>> f.close() >>> namelist1801txt