如何使用urllib模块读取中文字符?

时间:2019-04-04 04:03:47

标签: python-3.x

我在线检查了一下,发现您需要utf-8解码和一个文本模式文件才能读取汉字,但是使用urllib模块我无法执行任何上述操作,这是代码:

import urllib.request

fileIn = urllib.request.urlopen('http://resource.data.one.gov.hk/mtr/data/mtr_lines_and_stations.csv')

bytestr = fileIn.read()
lines = bytestr.decode('utf-8')
linesep = lines.split('\r\n')

name = []
element = []

for e in linesep:
    if e != '':
        clientRec = e.split(',')
        name.append(clientRec)

print(name)

那么如何允许它在我的姓名列表中保存汉字? 这是打印的错误:

Traceback (most recent call last):
  File "E:\School\Eclipse\GroupAssig\Resources\Code.py", line 23, in <module>
    print(name)
  File "C:\Program Files (x86)\Python37-32\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 154-156: character maps to <undefined>

0 个答案:

没有答案