Python Forum
replace bytes with other byte or bytes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
replace bytes with other byte or bytes
#2
(Feb-02-2019, 10:23 PM)BigOldArt Wrote: Am trying to replace all occurrences of bytes with other byte or bytes.
replace() dos this.
>>> myanswer = b"line1\rline2\rline3\rline4"
>>> myanswer.replace(b'\r', b'~')
b'line1~line2~line3~line4'
If want it to be string,also default text(Unicode) in Python 3.
>>> myanswer = b"line1\rline2\rline3\rline4"
>>> myanswer = myanswer.decode() # Same as decode('utf-8')
>>> myanswer
'line1\rline2\rline3\rline4'
>>> myanswer.replace('\r', '~')
'line1~line2~line3~line4'
Reply


Messages In This Thread
RE: replace bytes with other byte or bytes - by snippsat - Feb-02-2019, 11:00 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Int.From_Bytes Method - Conversion of Non-Escaped Bytes Objects new_coder_231013 1 270 Jan-17-2024, 09:13 PM
Last Post: Gribouillis
  Error on import: SyntaxError: source code string cannot contain null bytes kirkwilliams2049 7 6,144 Aug-03-2023, 06:00 PM
Last Post: Gribouillis
  Change HID bytes using pywinusb.hid Stealthrt 0 589 Jul-06-2023, 03:36 PM
Last Post: Stealthrt
  TypeError: a bytes-like object is required ZeroX 13 3,834 Jan-07-2023, 07:02 PM
Last Post: deanhystad
  TypeError: a bytes-like object is required, not 'str' - Help Please. IanJ 3 4,670 Aug-29-2022, 05:53 PM
Last Post: deanhystad
  Issue in changing data format (2 bytes) into a 16 bit data. GiggsB 11 2,559 Jul-25-2022, 03:19 PM
Last Post: deanhystad
Question How to understand the received bytes of ser.read? pf2022 3 1,918 Mar-24-2022, 11:37 AM
Last Post: pf2022
  bytes object saved as .mp4 jttolleson 10 5,745 Feb-25-2022, 02:42 PM
Last Post: jttolleson
  How to convert 4 bytes to an integer ? GiggsB 11 6,590 Jan-20-2022, 03:37 AM
Last Post: GiggsB
  Get amount of bytes in a file chesschaser 1 1,543 Aug-23-2021, 03:24 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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