您能否读取文件中一行开头的空格或制表符



所以我一直在尝试查看是否可以找到文件的制表符或空格。我现在使用它,我无法弄清楚我应该如何调用空格或选项卡或如何找到它。

file = open("file.txt","r")
    for space in line:
        if line.find("    "):
            print("This is something I want to add to the line",line)
#line in print is the rest of that line that has a tab or spaces in front of the text.

文件数据:

Names: / Status: 
John
      unavailable 
          unavailable
Mike
      available
Jack
      available

"状态"列中的选项卡我需要找到一种方法来读取它们。一行可能有多个选项卡。

您正在寻找类似以下内容:

file = open("file.txt","r")
    for space in line:
        idx = line.find("    ")
        if idx:
            print("This is something I want to add to the line", idx) 
        # Not sure what you want at the end here, but idx will give you the index
你应该

使用line.find("t")来查找选项卡

最新更新