Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check carriage return is there in a given string

i,m reading some lines from a file and i'm checking whether each line has windows type of CRLF or not. If either '\n' or '\r' is absent in any line, it has to report an error. I tried with the below code, even if the line doesnt have '\r', it is not reporting any error

Open_file = open(File_Name,'r').readlines()
while Loop_Counter!= Last_Line:
        Line_Read = Open_file[Loop_Counter]
        if('\r\n' in Line_Read):
            pass
        else:
            print Loop_Counter

Thank you

like image 788
user1915934 Avatar asked Dec 27 '22 12:12

user1915934


1 Answers

This isn't working because Loop_Counter is never adjusted at all; whatever the initial value is, it's not changing and the while loop either runs indefinitely or never passes. Your code is pretty unclear here; I'm not sure why you'd structure it that way.

What you're suggesting would be easier to do like this:

infile = open(filename, 'rb')
for index, line in enumerate(infile.readlines()):
    if line[-2:] != '\r\n':
        print index

The 'rb' argument is necessary to make sure the newlines are read as \r\n and not just as \n.

like image 178
jdotjdot Avatar answered Feb 01 '23 07:02

jdotjdot