简体   繁体   English

如何检查文件的每一行是否以空格开头?

[英]How to check if each line of a file starts with a whitespace?

I'm trying to create a function that iterates through each line of a code file and checks to see if each line starts white a whitespace. 我正在尝试创建一个遍历代码文件每一行的函数,并检查每一行是否以空白开头。

  # open file for reading
file = open('FileHandle', 'r')

# iterates through each line in file 
for aline in file.readlines():
    # splits each line in file into a separate line
    values = aline.split()
    # removes whitespaces that have been unintentionally added
    values = aline.rstrip() 
    # iterates through each line in file
    for values in aline:
        if values.startswith(' ') == True:
            # the first chacter is a space
            print 'Contains a line that starts with a space.'
        # checks if first character is something other than a space
        if values.startswith(' ') == False:
            # the first character is something other than a space
            # since blank lines contain no characters (not even spaces), this will still
            # be true boolean since '' is not == to ' '. 
            print 'Lines in file do not start with whitespace.'

I keep just getting multiple printed statements instead of a single concise statement, even if 1 line starts with a whitespace to print 'Contains a line that starts with a space.'. 即使1行以空格开头来打印“包含以空格开头的行”,我也只会得到多个打印的语句,而不是一个简洁的语句。 I'm assuming this has to do with that that my print statements are in the loop. 我假设这与我的打印语句在循环中有关。

The problem is because you are printing from within the loop. 问题是因为您正在循环中进行打印。 Instead, you can store the results in a variable and print after the loop: 相反,您可以将结果存储在变量中并在循环后打印:

has_line_starting_with_space = False
for values in aline:
    if values.startswith(' '):
        has_line_starting_with_space = True
        # no need to continue processing
        break
if has_line_starting_with_space:
    print 'Contains a line that starts with a space.'
else:
    print 'Lines in file do not start with whitespace.'

Note: this only handles space character and not other types of whitespace such as tabs. 注意:这仅处理空格字符,而不处理其他类型的空格,例如制表符。 To cover those cases, you can use the re module. 要解决这些情况,可以使用re模块。

Its pretty simple..All you have to do is Just check the if condition only with startswith function, You don't need that should check with "== true".. 它非常简单..您所要做的就是仅使用startswith函数检查if条件,您不需要使用“ == true”进行检查。

Code is: 代码是:

with open("file","r") as readfile:
    for line in readfile:
        if line.startswith( ' ' ):
            print "Contains a line that starts with a space."
        else:
            print "Lines in file do not start with whitespace."

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检测在 Python 中打开的文件的每一行中的第一个非空白字符? - How can I detect the first non-whitespace character in each line of a file opened in Python? 如何检查UTF-8文件的每一行并写入另一个文件? - How to check each line of file for UTF-8 and write in another file? 检查文件中的行是否以特定字符串开头-进行一些计算 - Check if a line in a file starts with a specific string - do some calculations 如何检查文本文件中每一行末尾的特定字符 - How do check for a specific character at the end of each line in a text file 如何从巨大的文本文件(> 16GB)中提取特定行,其中每行以另一个输入文件中指定的字符串开头? - How to extract specific lines from a huge text file (>16GB) where each line starts with a string specified in another input file? 如何检查python中的一行是否以单词或制表符或空格开头? - How to check whether a line starts with a word or tab or white space in python? 对于文件中的每一行,用换行符替换可变长的多个空白子字符串 - For each line in a file, replace multiple-whitespace substring of variable length with line break Numpy genfromtxt跳过每一行中的前导空格 - Numpy genfromtxt skipping leading whitespace in each line 如何检查文本文件的每一行是否有字符串并将所有出现该单词的行打印到新文本文件中? - How to check each line of a text file for a string and print all lines with occurrences of that word to a new text file? Python:如何检查文本文件,将其与另一个文本文件中的每一行进行比较,并打印不匹配的行 - Python: How to check a text file, compare it to each line in another text file, and print the lines that do not match
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM