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)
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)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With