Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 replace bytes with other byte or bytes
Am trying to replace all occurrences of bytes with other byte or bytes.
In the variable myvalue I want to replace badstuff with goodstuff every time it occurs

PyPdf2 returns a value like this from a multi line text box in a fillable PDF.

However, I need to write this without the quotes to a .TXT file
or like this if it has to be the same number of bytes

the example syntax below gets this message
Syntax Error: invalid syntax: <string>, line 5, pos 5

myanswer =b"line1\rline2\rline3\rline4"
print (myanswer)
badstuff = b"\r"
goodstuff = b"~"
if in(myanswer,badstuff):
print (myanswer)
(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'~')
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
>>> myanswer.replace('\r', '~')

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  I dont understand bytes in python. blackknite 3 289 Oct-02-2019, 07:39 PM
Last Post: Gribouillis
  Replace last 4 bytes of a timestamp mPlummers 18 788 Sep-17-2019, 12:47 AM
Last Post: Larz60+
  8-bit integer to bytes Garitron 1 259 Sep-14-2019, 11:22 PM
Last Post: ichabod801
  'utf-8' codec can't decode byte 0xda in position 184: invalid continuation byte karkas 7 4,723 Sep-12-2019, 11:19 PM
Last Post: newbieAuggie2019
  bytes not being printed as expected Skaperen 2 224 Aug-27-2019, 05:33 AM
Last Post: Skaperen
  understanding output of bytes/raw data rootVIII 3 392 Aug-01-2019, 01:00 PM
Last Post: rootVIII
  pickle docs say bytes in one place, strings in another Skaperen 2 228 Jul-29-2019, 05:13 PM
Last Post: Skaperen
  printing a bytes string Skaperen 2 262 Jul-21-2019, 03:42 AM
Last Post: Skaperen
  bytes-like object is required, not 'str anna 2 3,213 May-15-2019, 07:01 AM
Last Post: anna
  Getting error "Type error-a bytes-like object..." mrapple2020 1 1,911 Apr-06-2019, 06:37 PM
Last Post: mrapple2020

Forum Jump:

Users browsing this thread: 1 Guest(s)