Python Forum
quick function to get indentation level
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
quick function to get indentation level
#1
i'm reading in a series of lines. it might be better to think of this as iterating over an iterator of lines. the lines may be indented just like reading python source. what i would like for a function or class to do is, given that line (not stripped), return the indentation sequence for it. that is not how many spaces it is indented but rather, is the spaces if the text is indented by exactly one space, regardless of the actual syntax in use (disregard syntax). if the next line un-indents to a level previously seen, its index level will be the same as that level. if the next line un-indents to a number of spaces different than seen before, it can return some error value or raise an exception.

example:
start #0
here #0
  alpha #1
  beta #1
   gamma #2
      delta #3
      epsilon #3
   zeta #2
   eta #2
   theta #2
  iota #1
     kappa #2
      lambda #3
  mu #1
nu #0
    xi #1
        omicron #2
      pi #invalid undentation
anyone know of such a function or class?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
quick function to get indentation level - by Skaperen - Oct-25-2019, 12:48 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020