Python Forum
Can't strip extra characters from Data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't strip extra characters from Data
#2
Coming from a socket it will always be bytes,can easily convert to string str type.
>>> data = b'/hello123/'
>>> type(data)
<class 'bytes'>
>>> 
>>> data = data.decode()
>>> type(data)
<class 'str'>
>>> data
'/hello123/'
>>> data.strip('/')
'hello123'
When the change from Python 2 to Python 3 happened they made a clear choice that bytes and string(Unicode) can not mix.
Can also do it like and only convert when needed.
>>> data = b'hello123/'
>>> type(data)
<class 'bytes'>
>>> data.strip(b'/')
b'hello123'
When convert to str it will be Unicode as all string in Python3 is that,
so in background is really doing this when use .decode()
data = data.decode('utf-8')
Reply


Messages In This Thread
RE: Can't strip extra characters from Data - by snippsat - Jan-09-2022, 04:35 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  extract only text strip byte array Pir8Radio 7 3,082 Nov-29-2022, 10:24 PM
Last Post: Pir8Radio
Smile please help me remove error for string.strip() jamie_01 3 1,266 Oct-14-2022, 07:48 AM
Last Post: Pedroski55
  strip() pprod 8 3,572 Feb-16-2021, 01:11 PM
Last Post: buran
  Remove escape characters / Unicode characters from string DreamingInsanity 5 14,000 May-15-2020, 01:37 PM
Last Post: snippsat
  Need help with code for my WS2812B (Neopixel) Led Strip Phibbl 1 2,814 Apr-08-2020, 07:18 PM
Last Post: deanhystad
  split strip issues 'NonType' casacafe 8 3,996 Oct-08-2019, 06:29 PM
Last Post: ichabod801
  removing spaces/tabs after used .strip() zarize 0 1,627 Sep-11-2019, 12:46 PM
Last Post: zarize
  strip off just newlines Skaperen 11 5,482 Jun-19-2019, 06:28 PM
Last Post: Skaperen
  strip space from end of a row of text ineuw 4 2,963 Apr-15-2019, 03:14 AM
Last Post: ineuw
  How to remove whitespace from a string when .replace and .strip do not work winnetrie 7 4,579 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