Python Forum
How to remove char from string??
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to remove char from string??
#1
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
Reply
#2
(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('@'))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thank you....Yes it is byte

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  remove gilberishs from a "string" kucingkembar 2 202 Mar-15-2024, 08:51 AM
Last Post: kucingkembar
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,054 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
Smile please help me remove error for string.strip() jamie_01 3 1,149 Oct-14-2022, 07:48 AM
Last Post: Pedroski55
  Remove a space between a string and variable in print sie 5 1,706 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  How do I remove spurious "." from a string? Zuhan 7 1,962 Apr-12-2022, 02:06 PM
Last Post: Pedroski55
  How to replace on char with another in a string? korenron 3 2,293 Dec-03-2020, 07:37 AM
Last Post: korenron
  Remove from end of string up to and including some character lbtdne 2 2,284 May-17-2020, 09:24 AM
Last Post: menator01
  Remove escape characters / Unicode characters from string DreamingInsanity 5 13,416 May-15-2020, 01:37 PM
Last Post: snippsat
  Highlight and remove specific string of text itsalmade 5 3,445 Dec-11-2019, 11:58 PM
Last Post: micseydel
  Sum char word dictionary RavCOder 3 2,289 Nov-08-2019, 01:32 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020