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 from end of string up to and including some character lbtdne 2 473 May-17-2020, 09:24 AM
Last Post: menator01
  Remove escape characters / Unicode characters from string DreamingInsanity 5 952 May-15-2020, 01:37 PM
Last Post: snippsat
  Highlight and remove specific string of text itsalmade 5 722 Dec-11-2019, 11:58 PM
Last Post: micseydel
  Sum char word dictionary RavCOder 3 458 Nov-08-2019, 01:32 PM
Last Post: perfringo
  Cannot Remove the Double Quotes on a Certain Word (String) Python BeautifulSoup soothsayerpg 5 2,282 Oct-27-2019, 09:53 AM
Last Post: newbieAuggie2019
  with input remove a string from the list konsular 3 592 Oct-12-2019, 09:25 AM
Last Post: konsular
  The use of escape char \ hishamzero1 2 695 Aug-12-2019, 10:20 PM
Last Post: hishamzero1
  remove string character from url jacklee26 10 1,920 Mar-25-2019, 03:56 PM
Last Post: Larz60+
  First non-repeating char and some errors. blackknite 1 582 Jan-06-2019, 02:19 PM
Last Post: stullis
  How to remove whitespace from a string when .replace and .strip do not work winnetrie 7 1,168 Jan-05-2019, 08:44 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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