Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Python decode from a specific string

Someone gave me this string: "Al BaÅ£á ̧©ah" (it's probably an Arabic name), and asked me to translate it (and a list of other similar strings) in Python.

This website converts it to "Al Baţḩah" with the operation "UTF8 Decode". I'm trying to do the same in Python, but is what I tried and my results. Most of the examples took a series of unicode bytes. I tried the "detect" to see exactly what it is he was giving me. If it's already UTF8, then I'm not sure what that website is convert it to.

import chardet
byte_string = b"\x61\x62\x63"
decoded_string = byte_string.decode("utf8")
print(decoded_string)
sourceText = "Al BaÅ£á ̧©ah"
sourceTextBytes = bytes(sourceText, 'utf-8')
print(chardet.detect(sourceTextBytes))
decoded_string2 = sourceTextBytes.decode("utf")
print("Result2=", decoded_string2)

Output of above:

abc
{'encoding': 'utf-8', 'confidence': 0.9690625, 'language': ''}
Result2= Al BaÅ£á ̧©ah

The output is same as the input. I've tried ascic, utf8, etc... as the parms for the decode statement.

Part 2 - Here's another weird one that the solution below didn't work for (these are subdivison names from an ISO document a colleague purchased.)

GÉTMdÉTMbÉTMy

Answer*

Draft saved
Draft discarded
Cancel
6
  • You're a genius! How did you arrive at that conclusion? Commented Nov 10, 2020 at 3:59
  • 1
    years and years of sadness Commented Nov 10, 2020 at 4:07
  • 1
    @NealWalters a bunch of people collected their experience from more years of sadness and cast them into the Python library ftfy (fixes text for you), which can go directly from garbled text: ftfy.fix_text("Al Baţḩah")'Al Baţḩah'. It's not error-free (because this task is basically a guessing problem), but it's pretty good. Commented Nov 10, 2020 at 8:24
  • @lenz - cool, I used that library. I had some that caused an error on the decode solution, but ftfy didn't decode them either. I put a "Part 2" in my question above for a weird one that wouldn't fix. Commented Nov 10, 2020 at 17:29
  • your second one is probably cp1252 mojibake: >>> s.encode('cp1252').decode('utf-8') 'Gədəbəy' Commented Nov 10, 2020 at 17:46

lang-py

AltStyle によって変換されたページ (->オリジナル) /