1

I want to decode a string, which is already encoded in UTF-8

"\x6d\xc3\xbc\x6c\x6c\x65\x72\x20\x69\x73\x74\x20\x63\x6f\x6f\x6c\x21"

I cannot find a way to decode a string without getting such a error message text_utf8 = text_utf8.decode("utf-8") AttributeError: 'str' object has no attribute 'decode'

Is there a way to force a string to decode?

EDIT: I can´t use a Bytestring because my program imports a string out of a text file

2
  • Is this what you are looking for: stackoverflow.com/a/65874499/9760731 Commented Jan 7, 2023 at 14:29
  • How are you reading the string from a file? Show your code. It should use open(filename, encoding='utf8'). Commented Jan 7, 2023 at 17:12

1 Answer 1

0

You can use bytes function

byte_string = bytes(text_utf8, "utf-8")
decoded_string = byte_string.decode("utf-8")
Sign up to request clarification or add additional context in comments.

1 Comment

This is just roundtripping without change. You'll get text_utf8 == decoded_string for any input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.