Parsing/Splitting Line

John Machin sjmachin at lexicon.net
Tue Nov 21 14:39:15 EST 2006


Manuel Kaufmann wrote:
> El Martes, 21 de Noviembre de 2006 02:59, acatejr at gmail.com escribió:
> > I have a text file and each line is a list of values.  The values are
> > not delimited, but every four characters is a value.  How do I get
> > python to split this kind of data?  Thanks.
>
> You can define a function very easy to make it. For example you can do:
>
> # split-line in 'n' characters
> import sys
>
> def splitLine(line, n):
>     """split line in 'n' characters"""
>     x = 0
>     y = 0
>     while line >= n:

The intent appears to be that "line" refers to a str object, while "n"
refers to an int object. Comparison of such disparate objects is
guaranteed to produce a reproducible (but not necessarily meaningful)
result.

For example:

| >>> '' > 2
| True

You need to reconsider what you really want to have happen when there
is a trailing short slice. Possibilities are:

(a) silently ignore it -- what I guess your intent was, but the least
attractive IMO
(b) raise an exception -- overkill IMO
(c) just tack it on the end (which is what your code is currently doing
*accidentally*) -- and mention this in the docs and let the caller do
what they want with it.

>     	y = x + n
>         if line[x:y] == '':
>             break
>         yield line[x:y]
>         x += n
>
> if __name__ == '__main__':
>     # i get the line-split from the command line
>     # but you can get it from a file
>     for x in splitLine(sys.argv[1], int(sys.argv[2])):
>         print x
>

HTH,
John




More information about the Python-list mailing list