Encode error - python 3.6

Asked

Viewed 279 times

1

Traceback (most recent call last):
  File "uns.py", line 64, in <module>expiration_date="2017-08-27",
  File "uns.py", line 53, in create_user print(response.text)
  File "F:\Python\Python36\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 1822-1823: character maps to <undefined>

some py.:

from uuid import uuid4
from hashlib import sha256
from urllib.parse import urlencode, parse_qsl
from requests.auth import AuthBase
import requests


API_URL = "https://web-stage.voxy.com/partner_api"
API_KEY = "vSMgpl4"
API_SECRET = "Oe9qFkHUqsDWhc"


class VoxyAuth(AuthBase):
    """Attaches HTTP Voxy Authentication Header to the given Request object."""
    def __init__(self, api_key, api_secret):
        self.api_key = api_key
        self.api_secret = api_secret

    def __call__(self, request):
        request.headers['Authorization'] = "Voxy {}:{}".format(
            API_KEY,
            self.generate_signature(request)
        )
        # request.headers['Content-Type'] = "application/x-www-form-urlencoded"
        return request

    def generate_signature(self, request):
        if request.body:
            data = parse_qsl(request.body)
        else:
            data = {}
        url_encoded_data = urlencode(sorted(data))
        concat_string_data = self.api_secret.encode("utf-8") + url_encoded_data.encode("utf-8")
        hashed_data = sha256(concat_string_data)
        return hashed_data.hexdigest()


def create_user(external_user_id, first_name, email_address, native_language, expiration_date, **additional):
    url = "{}/partners/users/{}".format(API_URL, external_user_id)
    data = {
        "external_user_id": external_user_id,
        "first_name": first_name,
        "email_address": email_address,
        "native_language": native_language,
        "expiration_date": expiration_date
    }
    data.update(additional)
    response = requests.post(url, data=data, auth=VoxyAuth(API_KEY, API_SECRET))
    print("========= CREATE USER ===========")
    print("created user with external_user_id: {}".format(external_user_id))
    print(url)
    print(response)
    print(response.text)
    print("========= CREATE USER ===========")


random_string = uuid4().hex

create_user(
    external_user_id="1059",
    first_name="Gringo",
    email_address="[email protected]",
    native_language="pt",
    expiration_date="2017-08-27",
)
  • tries to put this at the beginning of your.py file #!/usr/bin/python&#xA;# -*- coding: iso-8859-15 -*- but choose the correct encoding for yours, more about this can be seen in: https://www.python.org/dev/peps/pep-0263/

  • Thanks Anderson and Armando. I’m new, @Armando how to choose the right meeting? I tested several and still with error: Syntaxerror: encoding problem: [ascii, cp1252, latin-1, utf-8]

  • Dude, I tell you, it’s been a while since I stopped using python, but first: vc gave a enter between the lines you added? it has to be a comment like this in each line and right at the beginning of the file, now as for the correct encoding, it will depend if you copied the file from the internet, then it is bad to know which was the encoding that it was created, you have to go checking, linux is easy, but if you use rWindows, chipped.

  • Yes, I gave the enter, this source was sent by a programmer who probably did on a mac, but just to understand the Ncode that is giving error is not of the characters of the program, but of the characters returned in the Answer of the request performed in the webservice Rest, right?

  • I was able to solve the question of the Encounter, it was only in the presentation 'print(Sponse.text.Ncode("utf-8"))'

  • 1

    People - please do not write nonsense - paste the encoding at the beginning of the file is not "Harry Potter magic" - and, yourselfer is required in Python 3. - It is also not runtime error: it gives error when you try to run the program.

  • To better understand encoding, without kicking "encodes" and "decodes' like crazy (in any language, not s'in Python), please read http://local.joelonsoftware.com/wiki/O_M%C3%Adnimo_absoluto_que_todos_programmers_de_software_absolutely need,_Absolutely,Positivamente_de_Saber_Sobre_Unicode_e_Conjuntos_de_Caracteres(Apologies!)

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.