Python how to trim a bytes string - python-3.x

I want to trim a bytes string before an index found by locating $$$,
trimmed_bytes_stream = padded_bytes_stream[:padded_stream.index('$$$')]
but got an error:
TypeError: a bytes-like object is required, not 'str'
Is there bytes object equivalent methods to do that? Or have convert bytes string to string and then using string methods? finally convert back to bytes after trimming?

Append a b to your search item
trimmed_bytes_stream = padded_bytes_stream[:padded_stream.index(b'$$$')]

Related

Convert string literal of bytes object to bytes

How can I convert a string like this: "b'this is something'" back to a bytes object?
Thank you.

How to ignore/delete undefined characters on string decoded

I'm reading a bus with bytes-like sequence of characters and i need to decode it in a string, but when I use decode method the output show undefined characters and i need to delete/ignore them.
Thanks all for help
I have already tried to use the method decode(encoding='utf-8', errors='ignore'), or with encoding='ascii', but I get same result.
x = ser.read_until(b'\x03', None)
string = x.decode(encoding='utf-8', errors='ignore')
This is the actual result: xx423711B552000083x (x = undefined character)
And I expected to have: 423711B552000083

How do I format this from str to byte-like object?

Ahoy, I'm having trouble with decoding these filenames (They're encoded as base64). I know they need to be byte-like objects, but I can't for the life of me make it so. Please help, much love.
for filename in os.listdir('./Files'):
name, typeId = base64.b64decode(filename.replace('.png', '')).split('_!_')
Error:
name, typeId = base64.b64decode(filename.replace('.png', '')).split('_!_')
TypeError: a bytes-like object is required, not 'str'
TypeError: a bytes-like object is required, not 'str'
You're probably going to get this error from two places:
b64decode(filename.replace('.png', ''))
As you've mentioned, b64decode expects a bytes-like object.
But filename is a str and filename.replace will also return a str.
.split('_!_')
Since b64decode will return bytes, you also have to pass a bytes-like object to split.
Try this:
for fname in os.listdir('./Files'):
fname_bytes = os.fsencode(fname.replace('.png', ''))
dec = base64.b64decode(fname_bytes)
parts = dec.split(b"_!_")
To solve 1., you can use fsencode as noted in the os.listdir docs:
Note: To encode str filenames to bytes, use fsencode().
To solve 2., you can prefix a "b" to the string to make it a byte literal:
Bytes literals are always prefixed with 'b' or 'B'; they produce an instance of the bytes type instead of the str type.

Why is this error appearing?

AttributeError: 'builtin_function_or_method' object has no attribute 'encode'
I'm trying to make a text to code converter as an example for an assignment and this is some code based off of some I found in my research,
import binascii
text = input('Message Input: ')
data = binascii.b2a_base64.encode(text)
text = binascii.a2b_base64.encode(data)
print (text), "<=>", repr(data)
data = binascii.b2a_uu(text)
text = binascii.a2b_uu(data)
print (text), "<=>", repr(data)
data = binascii.b2a_hqx(text)
text = binascii.a2b_hqx(data)
print (text), "<=>", repr(data)
can anyone help me get it working? it's supposed to take an input in and then convert it into hex and others and display those...
I am using Python 3.6 but I am also a little out of practice...
TL;DR:
data = binascii.b2a_base64(text.encode())
text = binascii.a2b_base64(data).decode()
print (text, "<=>", repr(data))
You've hit on a common problem in the Python3 - str object vs bytes object. The bytes object contains sequence of bytes. One byte can contain any number from 0 to 255. Usually those number are translated through the ASCII table into a characters like english letters. Usually in the Python you should use bytes for working with binary data.
On the other hand the str object contains sequence of code points. One code point usually represent one character printed on your screen when you call print. Internally it is sequence of bytes so the Chinese symbol 的 is internally saved as 3 bytes long sequence.
Now to the your problem. The function requires as input the bytes object but you've got a str object from the function input. To convert str into bytes you have to call str.encode() method on the str object.
data = binascii.b2a_base64(text.encode())
Your original call binascii.b2a_base64.encode(text) means call method encode of the object binascii.b2a_base64 with parameter text.
The function binascii.b2a_base64 returns bytes contains original input encoded with the base64 algorithms. Now to get back the original str from encoded data you have to call this:
# Take base64 encoded data and return it decoded as bytes object
decoded_data = binascii.a2b_base64(data)
# Convert bytes object into str
text = decoded_data.decode()
It can be written as one line
decoded_data = binascii.a2b_base64(data).decode()
WARNING: Your call of print is invalid for Python 3 (it will work only in the python console)

Converting a string to and from Base 64 [duplicate]

This question already has answers here:
Why do I need 'b' to encode a string with Base64?
(5 answers)
Closed 6 years ago.
I am trying to write two programs one that converts a string to base64 and then another that takes a base64 encoded string and converts it back to a string.
so far i cant get past the base64 encoding part as i keep getting the error
TypeError: expected bytes, not str
my code looks like this so far
def convertToBase64(stringToBeEncoded):
import base64
EncodedString= base64.b64encode(stringToBeEncoded)
return(EncodedString)
A string is already 'decoded', thus the str class has no 'decode' function.Thus:
AttributeError: type object 'str' has no attribute 'decode'
If you want to decode a byte array and turn it into a string call:
the_thing.decode(encoding)
If you want to encode a string (turn it into a byte array) call:
the_string.encode(encoding)
In terms of the base 64 stuff:
Using 'base64' as the value for encoding above yields the error:
LookupError: unknown encoding: base64
Open a console and type in the following:
import base64
help(base64)
You will see that base64 has two very handy functions, namely b64decode and b64encode. b64 decode returns a byte array and b64encode requires a bytes array.
To convert a string into it's base64 representation you first need to convert it to bytes. I like utf-8 but use whatever encoding you need...
import base64
def stringToBase64(s):
return base64.b64encode(s.encode('utf-8'))
def base64ToString(b):
return base64.b64decode(b).decode('utf-8')

Resources