'Python except for UnicodeError?
In my code I keep getting this error...
UnicodeEncodeError: 'charmap' codec can't encode character '\u2013' in position 390: character maps to <undefined>
I tried to put an except for UnicodeError and UnicodeEncodeError but nothing works, the problem is it's the users input so I can't control what they put so I need all encode errors to display a print that says error instead of crashing the program...
try:
argslistcheck = argslist[0]
if argslistcheck[0:7] != "http://":
argslist[0] = "http://" + argslist[0]
with urllib.request.urlopen(argslist[0]) as url:
source = url.read()
source = str(source, "utf8")
except urllib.error.URLError:
print("Couln't connect")
source = ""
except UnicodeEncodeError:
print("There was an error encrypting...")
source = ""
Traceback:
Traceback (most recent call last):
..... things leading up to error
File "C:\path", line 99, in grab print(source)
File "C:\Python33\lib\encodings\cp437.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2013' in position 390: character maps to <undefined>
Solution 1:[1]
Your print is failing. Your Windows console doesn't support printing UTF-8, you need to change the codepage:
chcp 65001
This is a Windows command, not a python command. You may need to switch fonts too, Lucida Sans Console is a Unicode font that can handle a lot more glyphs.
Solution 2:[2]
try this one to replace str():
source = source.encode('UTF-8')
Solution 3:[3]
start_url="https://www.indeed.co.in/jobs?q=teacher&l=India"
page_data=requests.get(start_url)
soup=BeautifulSoup(page_data.text,"lxml")
fname='1download'
with open(fname,'w')as f:
f.write(soup.prettify())
f.close()
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u20b9' in position
235677: character maps to <undefined>
Both errors can be solved with adding utf-8 as encoding to file.also please note you have to use with open( ) method to open files
f=open() will also give you error
Here is the correct code:
start_url="https://www.indeed.co.in/jobs?q=teacher&l=India"
page_data=requests.get(start_url)
soup=BeautifulSoup(page_data.text,"lxml")
fname='1download'
with open(fname,'w',encoding="utf-8")as f:
f.write(soup.prettify())
f.close()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Martijn Pieters |
| Solution 2 | wendong |
| Solution 3 |
