简体   繁体   中英

Python, UnicodeEncodeError: 'charmap' codec can't encode characters in position

I want to write the HTML of a website to the file I created, tough I decode to utf-8 but still it puts up a error like this, I use print(data1) and the html is printed properlyand I am using python 3.5.0

import re
import urllib.request

city = input("city name")   
url = "http://www.weather-forecast.com/locations/"+city+"/forecasts/latest"
data  = urllib.request.urlopen(url).read()
data1 = data.decode("utf-8")
f = open("C:\\Users\\Gopal\\Desktop\\test\\scrape.txt","w")
f.write(data1)

You've opened a file with the default system encoding :

f = open("C:\\Users\\Gopal\\Desktop\\test\\scrape.txt", "w")

You need to specify your encoding explicitly:

f = open("C:\\Users\\Gopal\\Desktop\\test\\scrape.txt", "w", encoding='utf8')

See the open() function documentation :

In text mode, if encoding is not specified the encoding used is platform dependent: locale.getpreferredencoding(False) is called to get the current locale encoding.

On your system, the default is a codec that cannot handle your data.

f = open("C:\\Users\\Gopal\\Desktop\\test\\scrape.txt","w",encoding='utf8')


f.write(data1)

This should work, it did for me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM