Unicodeencodeerror Latin 1

Unicodeencodeerror Latin 1




⚡ 👉🏻👉🏻👉🏻 INFORMATION AVAILABLE CLICK HERE 👈🏻👈🏻👈🏻




















































xjsender opened this issue on 20 Dec 2013 · 7 comments
xjsender opened this issue on 20 Dec 2013 · 7 comments
Requests is the latest version.
When I try to post the data which contains Chinese character, this exception is thrown.
Did you write X because that's a path to a local file? If so, your directory structure may be confusing urllib3. If not, then you should probably raise this with on bugs.python.org since this is not something I think requests should be handling. This looks like it's rising from httplib (or http on Python 3 which I'm guessing you're using).
I used requests in sublime plugin, if the soap_body in below statement didn't contains any Chinese characters, there will be no exception.
response = requests.post(self.apex_url, soap_body, verify=False, headers=headers)
Firstly, unless you're using a different version of Sublime Apex to the one in their public repository, Requests is not the latest version, it's version 1.2.3. Can you tell me what version of Sublime Text you're using?
So, ST 3, but not the most recent revision. Ok, that gives us something. Specifically, Sublime Text 3 uses Python 3.3, not Python 2.7 (which Sublime Text 2 used). This means all the default strings in Sublime Apex are unicode strings.
If you open up the Python 3.3 http.client file, you'll find that the _send_request() function looks like this:
# Honor explicitly requested Host: and Accept-Encoding: headers.
header_names = dict.fromkeys([k.lower() for k in headers])
skips = {}
if 'host' in header_names:
skips['skip_host'] = 1
if 'accept-encoding' in header_names:
skips['skip_accept_encoding'] = 1

self.putrequest(method, url, **skips)

if body is not None and ('content-length' not in header_names):
self._set_content_length(body)
for hdr, value in headers.items():
self.putheader(hdr, value)
if isinstance(body, str):
# RFC 2616 Section 3.7.1 says that text default has a
# default charset of iso-8859-1.
body = body.encode('iso-8859-1')
self.endheaders(body)
Now, ISO-8859-1 is an alias for Latin-1, which is the codec we're having trouble with. The problem we've got is that Sublime Apex is providing a unicode string body to Requests, which httplib needs to encode into bytes. Taking the default from RFC 2616, it concludes you want Latin-1, which doesn't include any Chinese characters. Clearly then, encoding fails, and you get the exception in question.
Considering that Sublime Apex claims in the headers it sends to be sending UTF-8 encoded data (which is a lie currently), Sublime Apex wants to be encoding the data as UTF-8 before sending it. This means any line sending data (in this case line 545 of salesforce/api.py) should read like this:
response = requests.post(self.apex_url, soap_body.encode('utf-8'), verify=False, headers=headers)
For the sake of anyone else who wants to confirm my diagnosis, here's a quick bit of sample code that confirms the problem:
a = "\u13E0\u19E0\u1320"
a.encode('latin1') # Throws UnicodeEncodeError, proves that this can't be expressed in ISO-8859-1.
a.encode('utf-8') # Totally fine.
r = requests.post('http://httpbin.org/post', data=a) # Using unicode string, throws UnicodeEncodeError blaming Latin1.
r = requests.post('http://httpbin.org/post', data=a.encode('utf-8')) # Works fine.
Thanks for raising this with us, but this is not a Requests bug. =)
r = requests.post('http://httpbin.org/post', data=a.encode('utf-8'))
very usefull,
thank you!
b3b mentioned this issue on 1 Dec 2018
angelajt mentioned this issue on 30 Jan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Successfully merging a pull request may close this issue.

Sign up or log in to view your list.
i am a newer in python.Today when I write some search function I met an error.well, I use sqlalchemy orm to do that, in my function,I input a chinese word as the key word.The html page give me an UnicodeEncodeError at /user/search:'latin-1' codec can't encode characters in position 0-1: ordinal not in range(256). and my code is like this:
when I met the error, I did a test in python shell, it worked well,the code is here:
from apps.user.models import User user = User.get_by_name('某人').first() print user print user.name 某人
so what can I do to let it worked in my html page?much appreciate!!
jiank
jiank 295●11 gold badge●33 silver badges●88 bronze badges
jpic
30.8k●33 gold badges●100100 silver badges●106106 bronze badges
I guess type of User.name is String? Try to change it to Unicode. – schlamar May 6 '13 at 14:29
I'm assuming that you're using MySQL with the MySQLdb driver here.
The default encoding used by the MySQLdb driver is latin-1, which does not support your character set. You'll need to use UTF-8 (or others, but UTF-8 is the most common) to be able to communicate with your database through MySQLdb (see http://docs.sqlalchemy.org/en/rel_0_8/dialects/mysql.html#unicode).
To do such a thing, create your engine with the following line:
You can also construct your engine url using the sqlalchemy.engine.url.URL class, and send it to the create engine function. I find it useful when you have your settings in a config file.
Balthazar Rouberol
Balthazar Rouberol 5,872●22 gold badges●3030 silver badges●4141 bronze badges
based on your stacktrace, you're using MySQL Python with unicode encoding turned on, since it's doing an encode. So you likely need to specify a comaptible encoding (note this is all settings used by the MySQLdb DBAPI, SQLalhcemy just passes them through):
zzzeek
zzzeek 61.8k●1818 gold badges●174174 silver badges●175175 bronze badges
Click here to upload your image (max 2 MiB)
You can also provide a link from the web.
By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy
2021 Stack Exchange, Inc. user contributions under cc by-sa
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Accept all cookies Customize settings

Xxx Jean Marie Ts
Nylon Feet Handjob
Korean Girl Bj
Ero Heels
Latin Beauties In High Heels
UnicodeEncodeError: 'latin-1' codec can't encode character ...
UnicodeEncodeError: 'latin-1' codec can't encode ...
python - UnicodeEncodeError:'latin-1' …
UnicodeEncodeError: 'latin-1' codec can't encode character ...
unicode - Python: UnicodeEncodeError: 'latin-1' codec can ...
Python: UnicodeEncodeError: кодек «latin-1» не может ...
UnicodeEncodeError: кодек latin-1 не может кодировать символ
Python: UnicodeEncodeError: 'latin-1' codec не может ...
python - Python: UnicodeEncodeError: кодек «latin-1» не ...
Unicodeencodeerror Latin 1


Report Page