检查回车是否在给定的字符串中

use*_*934 2 python python-2.7

我,从文件中读取一些行,我正在检查每行是否具有CRLF的窗口类型.如果任何行中不存在'\n'或'\ r',则必须报告错误.我尝试使用下面的代码,即使该行没有'\ r',也没有报告任何错误

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
Run Code Online (Sandbox Code Playgroud)

谢谢

jdo*_*dot 8

这是行不通的,因为Loop_Counter根本没有调整过; 无论初始值是什么,它都没有改变,while循环无论是无限运行还是永不通过.你的代码在这里很不清楚; 我不确定你为什么要这样构造它.

您建议的更容易做到这样:

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

这个'rb'论点是必要的,以确保读取新行,\r\n而不是作为\n.