Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python byte string

#A byte string can be decoded back into a character string, 
#if you know the encoding that was used to encode it.
b'I am a string'.decode('ASCII')
b'I am a string'.decode('utf-8')
Comment

byte strings python

It is a common misconception that text is ascii or utf8 or cp1252, and therefore bytes are text.
Text is only text, in the way that images are only images. 
The matter of storing text or images to disk is a matter of encoding that data into a sequence of bytes. 
There are many ways to encode images into bytes: Jpeg, png, svg, and likewise many ways to encode text, ascii, utf8 or cp1252.
Once encoding has happened, bytes are just bytes. 
Bytes are not images anymore,they have forgotten the colors they mean; 
although an image format decoder can recover that information.
Bytes have similarly forgotten the letters they used to be. 
In fact, bytes don't remember wether they were images or text at all. Only out of band knowledge 
(filename, media headers, etcetera) can guess what those bytes should mean, and even that can be wrong (in case of data corruption)
so, in python (py3), we have two types for things that might otherwise look similar; 
For text, we have str, which knows it's text; it knows which letters it's supposed to mean. 
It doesn't know which bytes that might be, since letters are not bytes. 
We also have bytestring, which doesn't know if it's text or images or any other kind of data.
The two types are superficially similar, since they are both sequences of things, but the things that they are sequences of is quite different.
Implementationally, str is stored in memory as UCS-? where the ? is implementation defined, 
it may be UCS4, UCS2 or UCS1, depending on compile time options and which codepoints are present in the represented string.
Comment

PREVIOUS NEXT
Code Example
Python :: convert matplotlib figure to cv2 image 
Python :: try catch python 
Python :: string to list python 
Python :: how do you write a function in python 
Python :: how to get the first key of a dictionary in python 
Python :: python pandas give column names 
Python :: calculate mean on python 
Python :: pandas date range 
Python :: python how to get the last element in a list 
Python :: how to run pyttsx3 in a loop 
Python :: filter one dataframe by another 
Python :: sklearn support vector machine 
Python :: find percentage of missing values in a column in python 
Python :: how to write in a text file python 
Python :: drop first two rows pandas 
Python :: set column datatype pandas 
Python :: pandas change dtype 
Python :: deleting models with sqlalchemy orm 
Python :: if list item in string python 
Python :: notion python api 
Python :: python search first occurrence in string 
Python :: droping Duplicates 
Python :: pandas copy data from a column to another 
Python :: python dictionary sort 
Python :: pytorch transpose 
Python :: reset_index(drop=true) 
Python :: copy only some columns to new dataframe in r 
Python :: merge two dataframes based on column 
Python :: python 3.8.5 download 32 bit 
Python :: install aws sdk python 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =