Python中字符串的一些解码问题

时间:2015-06-27 13:30:47

标签: python python-3.x unicode unicode-string

我正在尝试将来自Google的HTML代码字符串写入Python 3.4中的文件

#coding=utf-8
try:
    from urllib.request import Request, urlopen  # Python 3
except:
    from urllib2 import Request, urlopen  # Python 2

useragent = 'Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0'

#Generate URL
url = 'https://www.google.com.tw/search?q='
query = str(input('Google It! :'))
full_url = url+query


#Request Data
data = Request(full_url)
data.add_header('User-Agent', useragent)
dataRequested = urlopen(data).read()
dataRequested = str(dataRequested.decode('utf-8'))


print(dataRequested)

#Write Data Into File
file = open('Google - '+query+'.html', 'w')
file.write(dataRequested)

它可以正确打印字符串, 但是当它写入文件时 它会显示

file.write(dataRequested)
UnicodeEncodeError: 'cp950' codec can't encode character '\u200e' in position 97658: illegal multibyte sequence

我尝试更改解码方式但不起作用。 我也试图替换\ u200e,但它会出现更多编码字符错误。

1 个答案:

答案 0 :(得分:1)

你的问题是

  

dataRequested = str(dataRequested.decode('utf-8'))

是否有理由将解码后的UTF-8转换为字符串?但这还不是全部。当您从Internet获取字符串时,它应该被解码,但是当您保存字符串时,它应该被编码。有些人不明白。它们可以解码或编码。它没有这种方式。

我改变了你的代码。它在Python2.7和Python3.4上都适合我。

dataRequested = dataRequested.decode('utf-8')


print(dataRequested)

#Write Data Into File
file = open('Google - '+query+'.html', 'wb')
file.write(dataRequested.encode('utf-8'))