Next / Previous / Contents / Shipman's homepage

6. Functions

These functions are available in the pyparsing module.

6.1. col(): Convert a position to a column number

pp.col(loc, s)

The loc argument to this function is the location (Python index, counted from 0) of some position in a string s. The returned value is the column number of that position within its line, counting from 1. Newlines ('\n') are treated as line separators.

>>> text = 'abc\nde\n'
>>> for k in range(len(text)):
...     print "Position {0}: col {1}, lineno {2}".format(
...         k, pp.col(k, text), pp.lineno(k, text))
... 
Position 0: col 1, lineno 1
Position 1: col 2, lineno 1
Position 2: col 3, lineno 1
Position 3: col 1, lineno 1
Position 4: col 1, lineno 2
Position 5: col 2, lineno 2
Position 6: col 1, lineno 2