Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upgrading scripts to Python 3 - \r\n & text binary mode

Really struggling with something that should be pretty basic. I'm looking to identify instances where \n does not have a reciprocal \r (i.e. \r\n is good x\n is bad and would ).

I appreciate this is because python 2 managed 'rb' differently but can't work out the equivalent function or way of identifying \r in python 3.

import re
import sys
import time
with open('4 - raw.txt', 'rb') as content_file:
    content = content_file.read()

newLinePos = [m.start() for m in re.finditer('\n', content)]

for line in newLinePos:
    if (content[line-1]) != '\r':
        print (repr(content[line-20:line]))

print ("end")
time.sleep(1000)
like image 216
Brodie Avatar asked Aug 07 '26 22:08

Brodie


1 Answers

Python 3 makes a clear distinction between raw byte strings, and utf-8 string. content[line-1] is returning a number, probably 0-255 - the byte, and you are trying to match it to a string, '\r'. I agree possibly the conversion could be made, but Python is strongly typed, so this will always fail, regardless of what character the integer represents. To get the byte number corresponding to \r use:

(content[line-1]) != ord('\r')

and similarly use a bytes string for your iterator generation:

newLinePos = [m.start() for m in re.finditer(b'\n', content)]
like image 96
kabanus Avatar answered Aug 09 '26 12:08

kabanus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!