Python Forum

Full Version: regex octal replace
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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')))