#๐Ÿ”’ Unicode encode error

45 messages ยท Page 1 of 1 (latest)

cloud fableBOT
#

@sick sapphire

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

sterile pivot
#

the content you're getting may not be JSON at all

#

I suggest you check what it actually is

stone oyster
#

Check both headers Content-Type and Content-Encoding

#

content-encoding may be absent

sterile pivot
#

and content-type may lie

#

though it's more likely that you're just getting non-json content like HTML

#

happened to me more times I can count

stone oyster
#

What's the url you're using?

sterile pivot
#

if you replace .json() with read(), can you verify that it's actually in JSON format?

stone oyster
#

.content

sterile pivot
#

oh, that's the way? I don't use requests myself

stone oyster
#

the error isn't in the json part, but decoding it from utf-8

snow goblet
#

Encode Error? that's weird though

sterile pivot
#

I wanted the OP to confirm the contents to see what they're really dealing with

#

yeah, it's apparently .content in requests

stone oyster
#

He's confusing requests with urllib.request

#

content will return a bytes object

sterile pivot
#

I don't use that either ๐Ÿ™‚

#

@sick sapphire seeing what the content is would help

#

emojis should be fine

#

well, utf-8 has no problem with encoding emojis

#

but...what is that charmap codec?

snow goblet
#

I'm more confused it's saying Unicode__Encode__Error.

sterile pivot
#

yeah, that's weird

#

that above sequence decodes to ๐Ÿ‘‘

#

@sick sapphire could you show us the traceback?

snow goblet
#

charmap could be something like cp1252.. it usually says on the next line of the error.
It would be helpful if you showed the full tracback.

stone oyster
#

!traceback please

cloud fableBOT
#
Traceback

Please provide the full traceback for your exception in order to help us identify your issue.
While the last line of the error message tells us what kind of error you got,
the full traceback will tell us which line, and other critical information to solve your problem.
Please avoid screenshots so we can copy and paste parts of the message.

A full traceback could look like:

Traceback (most recent call last):
  File "my_file.py", line 5, in <module>
    add_three("6")
  File "my_file.py", line 2, in add_three
    a = num + 3
        ~~~~^~~
TypeError: can only concatenate str (not "int") to str

If the traceback is long, use our pastebin.

sterile pivot
#

I bet that OP looked at the wrong line of the traceback

#

yup, I was right

stone oyster
#

it's an error with print

#

likely your stdout is misconfigured

sterile pivot
#

why did you say it was a problem with requests?

#

how?

stone oyster
#

You can do sys.stdout.reconfigure(encoding="utf-8")

sterile pivot
#

when clearly it happened when printing something to the console

stone oyster
#

I've seen some installs use ascii for stdout for some reason

#

you can check via sys.stdout.encoding

#

!e import sys; print(sys.stdout.encoding)

cloud fableBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

#

๐Ÿ”’ Unicode encode error