how to modify row content of file in python?

Simon Hibbs simon.hibbs at
Tue Oct 27 14:12:58 CET 2009

On 27 Oct, 07:55, holmes86 <holme... at> wrote:
> hi,everyone
> Is there function of modify row content in python? just like 'sed' in
> shell,thanks

You would do something like this, which acepts a CSV file from stdin
and sends the first column to stdout (prints it), which you could
redirect to a file or pipe to another command.

import sys

with infile = sys.stdin.readlines():
    for line in infile.readlines():
        columns = line.split(',')
        print columns[0]

This code is not tested as Windows Explorer just crashed on my box
(effing windows - I'm at work so no Mac. *sob*).

One thing to look out for is that 'columns' is being created each time
through the loop, which works fine. If you modified the code to append
to it instead, you would need to create it before entering the loop as
you can't append to something that doesn't exist.

Simon Hibbs

More information about the Python-list mailing list