how to modify row content of file in python?
Simon Hibbs
simon.hibbs at gmail.com
Tue Oct 27 09:12:58 EDT 2009
On 27 Oct, 07:55, holmes86 <holme... at gmail.com> 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