你能读一下文件中一行开头的空格或制表符吗?

时间:2018-03-05 05:35:23

标签: python file for-loop if-statement printing

所以我一直试图查看是否可以找到文件的标签或空格。我现在使用它,我无法弄清楚我应该如何调用空格或标签或如何找到它。

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

“状态”列中的标签我需要找到一种方法来阅读它们。一行可能有多个标签。

2 个答案:

答案 0 :(得分:2)

您应该使用line.find("\t")查找标签

答案 1 :(得分:2)

你正在寻找这样的东西:

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