Parsing/Splitting Line

Manuel Kaufmann humitos at gmail.com
Tue Nov 21 10:17:30 EST 2006


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:
    	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



-- 
Kaufmann Manuel



More information about the Python-list mailing list