Coder Social home page Coder Social logo

inab's Introduction

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")

text = arabic_de(x)

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)

inab's People

Contributors

contriveone avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.