Python Forum

Full Version: How to remove char from string??
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Good morning....

I have a string being returned '@S0601\r\n'.....I am trying to remove the @ at the front using .replace and .lstrip but to no avail....Below is my lastest code try with error that I seem to get on both .replace or .lstrip....Can someone tell me what I am doing wrong?

print((s.read(int(k)).rstrip()).lstrip('@'))
Error:
Traceback (most recent call last): File "radar.py", line 16, in <module> print((s.read(int(k)).rstrip()).lstrip('@')) TypeError: a bytes-like object is required, not 'str'
Thanks
(Sep-30-2020, 01:17 PM)ridgerunnersjw Wrote: [ -> ]I have a string being returned '@S0601\r\n'
I guess it's not a str object but bytes-like object, given that you have s.read(int(k)) - we don't know what s is...

foo = b'@S0601\r\n'
print(foo.rstrip().lstrip(b'@').decode())
print(foo.decode().rstrip().lstrip('@'))
Thank you....Yes it is byte

adding the b in .lstrip(b'@') seemed to solve it