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
Kevin Hu
hxy9243 at gmail.com
Thu Nov 8 22:47:59 EST 2018
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 <https://docs.python.org/3/howto/unicode.html>
You can simply add:
namelist_decoded = namelist1801txt.decode(‘utf-8’)
Which should give you the actual characters:
> 陈巍\n胡智健\r黄坤榕\r李星灿\r刘超杰
Some tutorials lying around Internet:
https://www.programiz.com/python-programming/methods/string/encode <https://www.programiz.com/python-programming/methods/string/encode>
https://www.pythoncentral.io/encoding-and-decoding-strings-in-python-3-x/ <https://www.pythoncentral.io/encoding-and-decoding-strings-in-python-3-x/>
Regards,
Kevin
> On Nov 8, 2018, at 21:30, Annie Lu <gabriella19930611 at gmail.com> wrote:
>
> # -*- coding: UTF-8 -*-
> ... f = open('/Users/annielu/Desktop/namelist1801.txt')
>>>> namelist1801txt = f.read()
>>>> f.close()
>>>> namelist1801txt
> '\xe9\x99\x88\xe5\xb7\x8d\n\xe8\x83\xa1\xe6\x99\xba\xe5\x81\xa5\r\xe9\xbb\x84\xe5\x9d\xa4\xe6\xa6\x95\r\xe6\x9d\x8e\xe6\x98\x9f\xe7\x81\xbf\r\xe5\x88\x98\xe8\xb6\x85\xe6\x9d\xb0\r\xe7\x8e\x8b\xe4\xbf\x8a\xe5\x80\xbc\r\xe4\xbd\x99\xe4\xb8\x9c\xe6\xbd\xae\r\xe9\x99\x88\xe6\x80\x9d\xe5\x87\xbd\r\xe5\x86\xaf\xe5\xb0\x91\xe5\x90\x9b\r\xe9\xbb\x84\xe5\x98\x89\xe8\xb0\x8a\r\xe9\xbb\x84\xe7\x90\xaa\xe7\x90\xaa\r\xe8\xb5\x96\xe5\xa9\x89\xe5\xa9\xb7\r\xe8\xb5\x96\xe5\xbd\xa6\xe9\x9c\x8f\r\xe5\xbb\x96\xe7\xbf\xa0\xe7\x9b\x88\r\xe6\x9e\x97\xe7\xbe\xbd\xe7\x8f\x82\r\xe5\x88\x98\xe5\xae\x89\xe7\x90\xaa\r\xe9\xa9\xac\xe7\x91\x9e\r\xe5\xbd\xad\xe5\x98\x89\xe4\xbb\xaa\r\xe9\x82\xb1\xe6\xaf\x93\xe4\xbb\xaa\r\xe5\xad\x99\xe6\xa3\xae\xe6\xa3\x8b\r\xe8\xb0\xad\xe5\x98\x89\xe7\x90\xaa\r\xe7\x8e\x8b\xe5\xa4\xa9\xe9\x9f\xb5\r\xe5\x90\xb4\xe5\xad\x90\xe7\x8f\xba\r\xe6\x9d\xa8\xe5\x88\xa9\xe8\x8c\xb5\r\xe5\xa7\x9a\xe5\x98\x89\xe9\x9b\xaf\r\xe8\xa2\x81\xe6\x9c\x88\xe6\xbb\xa2\r\xe5\xbc\xa0\xe9\x87\x87\xe7\x8e\
> x89\r\xe5\xbc\xa0\xe6\xb2\x81\xe7\x8e\xa5'
>>>>
> --
> https://mail.python.org/mailman/listinfo/python-list
More information about the Python-list
mailing list