Skip to main content

A library for decoding a byte marked as a string

Project description

When reading a file containing bytes using Python, you are often looking to read it like this b"\xd8\xa7\xd9\x84\xd8\xa8\xd9\x84\xd9\x8a\xd8\xba, for example o = open("bytes.txt", "rb") y = x.read() In fact, the exit is like this b"\\xd8\\xa7\\xd9\\x84\\xd8\\xa8\\xd9\\x84\\xd9\\x8a\\xd8\\xba, and when you try to decode it, you will get this back \xd8\xa7\xd9\x84\xd8\xa8\xd9\x84\xd9\x8a\xd8\xba, but it is a literal type, and whenever you code it, it will give you two slashback, and when you decode it, it takes you back to \xd8\xa7\xd9\x84\xd8\xa8\xd9\x84\xd9\x8a\xd8\xba. This problem I encountered when I was scraping a website and recving the data in bytes in a string type that included HTML. and it will face you in the future or you may have encountered before, I recently built a library to solve this problem where it is enough to pass it a byte with two slashback and it will decode it to the original text.

decode arabic utf-8

from inab import arabic_de str_bytes = "\\xd8\\xa7\\xd9\\x84\\xd8\\xa8\\xd9\\x84\\xd9\\x8a\\xd8\\xba" x = bytes(str_bytes, "utf-8") print(x) text = arabic_de(x) print(text)

decode english utf-16

from inab import eng_de str_bytes = "\\xff\\xfei\\x00n\\x00a\\x00b\\x00 \\x00l\\x00i\\x00b\\x00" text = eng_de(str_bytes) print(text)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

inab-0.0.1.tar.gz (1.5 kB view hashes)

Uploaded Source

Built Distribution

inab-0.0.1-py3-none-any.whl (1.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page