1. 程式人生 > >【Python】3.x 版本的print 亂碼 問題

【Python】3.x 版本的print 亂碼 問題

奇怪,之前發的博文中內容怎麼沒有了。最近剛開始用csdn,發現經常丟東西,不明白。。

import urllib.request 

url = 'http://www.baidu.com'  

request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
data = response.read()

datautf = data.decode('UTF-8')
fo = open('log.html', 'w',encoding='utf-8')
fo.write(datautf)
fo.close()

text = open('log.html', encoding='utf8').read() 
print(text.encode('utf8'))

至此,Python3 中 print 問題解決。