Python Forum
UnicodeEncodeError using Windows and IDE-Visual Studio
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UnicodeEncodeError using Windows and IDE-Visual Studio
#1
I am new to Python and excited to learn and just hit the ground.
I have Windows and use Visual Studio as IDE. I have got everything set and started to practice with few lines of code. I have now hit a stumbled block.
I have got these few lines from a web site.
import requests
result= requests.get('https://www.nytimes.com/interactive/2017/06/23/opinion/trumps-lies.html')
print(len(result))
print(result [0:3] )
print(result)
The first line works- print(len(result)), but I keep getting an error on the other two lines.
I have done lots of search on-line, but I could not resolve this issue.
The issue seems to be arisen from windows, not Python as Windows does not support Unicode.
There are many suggestions on how to resolve the issue, but all seem to be a short cut or short-term solutions. Others suggested I should upgrade to Python 3.6 (I have 3.5).
https://groups.google.com/forum/#!topic/...0J2X-dHb8k

I really have no idea how to correct this error and my enthusiasm for Python might be short lived.
Please help.
Reply
#2
Use BBCode to post.

Heres my code (python 3.6):
>>> import requests
>>> result= requests.get('https://www.nytimes.com/interactive/2017/06/23/opinion/trumps-lies.html')
>>> print(result.text)
It works fine when I use text attribute of Response Object.

T get an error when I try to use Response object as the content of the request.

>>> print(result)
<Response [200]>
>>> print(len(result))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object of type 'Response' has no len()
but the length of the content works fine.
>>> print(len(result.text))
161117
Reply
#3
Please post your error traceback (all of it) as it contains valuable information
also, change:
print(result)
# to
print (result.text)
finally, when posting code use code tags. See BBCODE
and use shift-ctrl-v when pasting code to preserve indentation
Reply
#4
Hi hbknjr,
Thanks for the reply!
Sorry! I do not seem to see the BB code. I though it is the Python symbol. Please show me which button is.
Yes, both length and response methods work, but not text. I have got the text property from the website.
http://www.dataschool.io/python-web-scra...umps-lies/
Although, it does not appear as properties in the auto- completion, I do not get a syntax error.
I have also got these properties work.

print(result.cookies)
print(result.headers)
print(result.elapsed)
print(result.history)
print(result.json)
print(result._next)
So, I am wondering if the error is the text property or windows console does not have Unicode characters.

Hi Larz60+,
Thanks for the reply!
The error does not appear to be consistent. The only line which appears on all errors is:
 File "C:\Users\Name\AppData\Local\Programs\Python\Python35-32\lib\encodings\cp850.py", line 19, in encode
The above line looks like a library.

 return codecs.charmap_encode(input,self.errors,encoding_map)[0]
#UnicodeEncodeError: 'charmap' codec can't encode characters in position 63893-63896: character maps to <undefined>
Another error

   return codecs.charmap_encode(input,self.errors,encoding_map)[0]
#UnicodeEncodeError: 'charmap' codec can't encode character '\u2014' in position 282: character maps to <undefined>
Reply
#5
A couple of things and questions. First thing is post the entire error code between tags (the red X next to the Python icon on the editor bar), posting one line is not helpful. Question, as a new Python user, why are you not using the latest Python version (currently 3.6.3). Those responses you got from from your search are correct, upgrading Python will eliminate that problem.

My advice is to uninstall your v3.5 and install the latest version. Follow our tutorial hereĀ https://python-forum.io/Thread-Basic-Par...er-Windows, make sure you obtain the correct file to match your machine, i.e.: 32 or 64 bit.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#6
sparkz_a lot,
I thought I used the BB code. My code appears to be the same as the others. If not, I apologise and will do it right next time.
Well, my code is 2 lines only. I am stuck on the second line. I have posted the code from this site.
http://www.dataschool.io/python-web-scraping-of-president-trumps-lies/
Reply
#7
This is the Python tags for your code. The Python icon
Error:
This is errors. The Red X
Output:
This is the output for your code. The black bar with the arrow above it
Your errors are referring to unicode errors, which Windows does not support. Windows uses 'code pages', in your case it is code page 850 (cp850). Python got tired of waiting for Windows to get on the utf-8 train and fixed it themselves
with version 3.6. This only affects Python. If you use the command terminal for other purposes, it will still use the default code page. So to resolve your problem with Python (regarding utf) upgrade your version.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#8
sparkz_a lot,
Thanks again!
So, the Python symbol is the "BB code"
Okay, I will do as per you suggestion- uninstall 3.5 and install the latest version.
Reply
#9
It is one of the icons.

If you follow the tutorial link I posted, you won't end up with that horrendous installation directory, an added bonus :-)
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#10
sparkz_a lot,
I followed the instructions and you are spot on. Installing it on top directory was a surprise and bonus. I had struggled with directory paths and names and not sure what directory was. I can now easily find Python and install Lib using pip. No more worries about paths and directories.
I think installing Python on some other root directory should be banned- well at least for beginners Smile . We have already got enough problems at hand- learning the language.

Thanks again for the all the help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to set PYTHONPATH in Visual Studio Code? aupres 5 4,347 Aug-15-2023, 03:51 PM
Last Post: snippsat
  how do I open two instances of visual studio code with the same folder? SuchUmami 3 915 Jun-26-2023, 09:40 AM
Last Post: snippsat
  Visual Studio Code NewPi 3 1,079 May-16-2023, 11:13 PM
Last Post: snippsat
Question UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 562: ord ctrldan 23 4,963 Apr-24-2023, 03:40 PM
Last Post: ctrldan
  [SOLVED] [Debian] UnicodeEncodeError: 'ascii' codec Winfried 1 1,042 Nov-16-2022, 11:41 AM
Last Post: Winfried
  Visual Studio Code venv ibm_db error mesi1000 7 2,857 Nov-13-2022, 12:36 AM
Last Post: snippsat
  debugging help on Visual Studio Code mg24 12 2,059 Oct-02-2022, 12:18 AM
Last Post: mg24
  UnicodeEncodeError - Dealing with Japanese Characters fioranosnake 2 2,508 Jul-07-2022, 08:43 PM
Last Post: fioranosnake
  Problem with importing Python file in Visual Studio Code DXav 7 5,190 Jun-15-2022, 12:54 PM
Last Post: snippsat
  UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in position 14: ordin Armandito 6 2,791 Apr-29-2022, 12:36 PM
Last Post: Armandito

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020