Welcome, guest | Sign In | My Account | Store | Cart

This is a bare-bones reader generator for file processing of typical Unix style configuration and data files.

Python, 9 lines
1
2
3
4
5
6
7
8
9
def dsvgen(filename,separator='|'):
    """generates a list of values for each line in a dsv file

    >>> for row in dsvgen('myfilename'): print row
    """
    inpfile=open(filename)
    for line in inpfile:
        yield line.strip().split(separator)
    inpfile.close()

There are a lot of delimited files out in the wild and for good reason: they are easy to process. This generator does not handle delimiter escaping, quoting, header rows or file errors but still works surprisingly well in many cases. For more advanced processing of delimited files see the csv module of the standard library. The benefit of this generator over the csv module is that it already includes the file open-close boilerplate and that it is dead simple.

Created by N N on Mon, 17 Dec 2007 (PSF)
Python recipes (4591)
N N's recipes (3)

Required Modules

  • (none specified)

Other Information and Tasks