Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
regex octal replace
#1
Octal Dump Of Text Read From File. (od -cbx filename)

. . . --> vvvvvvvvvvv
0001540 m i l y \n \n < P > \n 342 200 234 W h
155 151 154 171 040 012 012 074 120 076 012 342 200 234 127 150
696d 796c 0a20 3c0a 3e50 e20a 9c80 6857
0001560 y a r e y o u d o i n g
171 040 141 162 145 040 171 157 165 040 144 157 151 156 147 040
2079 7261 2065 6f79 2075 6f64 6e69 2067
. . .


Trying to use the following code to replace the non-printing sequence of 3 bytes with a single quote character ("):

text = re.sub(r'\342\200\234', r'"', text)
It has no effect on the text as if it never finds a match.
Reply
#2
I would try to solve your task using split and join approach, e.g.:

with open('output_file.dat', 'wb') as out_f:
    with open('input_file.dat', 'rb') as inp_f:
        out_f = out_f.write(b'"'.join(inp_f.read().split(b'\x03\x05\x08')))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Regex replace in SQLite3 database WJSwan 1 749 Dec-04-2023, 05:55 PM
Last Post: Larz60+
  python-docx regex: replace any word in docx text Tmagpy 4 2,139 Jun-18-2022, 09:12 AM
Last Post: Tmagpy
  Find and replace in files with regex and Python Melcu54 0 1,825 Jun-03-2021, 09:33 AM
Last Post: Melcu54
  Find and replace to capitalize with Regex hermobot 2 2,483 Mar-21-2020, 12:30 PM
Last Post: hermobot
  Regex won't replace character with line break Tomf96 2 2,501 Jan-12-2020, 12:14 PM
Last Post: Tomf96
  Search & Replace - Newlines Added After Replace dj99 3 3,356 Jul-22-2018, 01:42 PM
Last Post: buran
  octal encoding Skaperen 4 3,892 Dec-06-2017, 02:53 AM
Last Post: Skaperen
  octal instead of hex Skaperen 4 3,834 Jun-06-2017, 02:00 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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