0

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ədəbəy

1 Answer 1

1

looks like a classic case of mojibake -- in this case it's interpreted using latin1 when it should be UTF-8:

>>> "Al Baţḩah".encode('latin1')
b'Al Ba\xc5\xa3\xe1\xb8\xa9ah'
>>> "Al Baţḩah".encode('latin1').decode('UTF-8')
'Al Baţḩah'

Code for those that want to copy/paste into a program instead of command line:

source_text = "Al Baţḩah"
print("source_text=", source_text)
encoded_source_text = source_text.encode('latin1')
decoded_text = encoded_source_text.decode('UTF-8')
print("decoded_text=", decoded_text)
Sign up to request clarification or add additional context in comments.

6 Comments

You're a genius! How did you arrive at that conclusion?
years and years of sadness
@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.
@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.
your second one is probably cp1252 mojibake: >>> s.encode('cp1252').decode('utf-8') 'Gədəbəy'
|

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.