how to modify row content of file in python?

Discussion in 'Python' started by holmes86, Oct 27, 2009.

  1. holmes86

    holmes86 Guest

    hi,everyone

    Is there function of modify row content in python? just like 'sed' in
    shell,thanks
     
    holmes86, Oct 27, 2009
    #1
    1. Advertising

  2. holmes86

    Lutz Horn Guest

    Hi,

    * holmes86 [Tue, 27 Oct 2009 00:55:36 -0700]:
    > Is there function of modify row content in python? just like 'sed' in
    > shell,thanks


    Sed doesn't actually modify the contents of a file. In normal operation
    mode it reads from STDIN and writes to STDOUT. Some versions of sed
    provide the -i option to

    edit files in place (makes backup if extension supplied)

    You could program a similar behaviour in Python using the re module.

    Lutz
     
    Lutz Horn, Oct 27, 2009
    #2
    1. Advertising

  3. En Tue, 27 Oct 2009 05:50:47 -0300, Lutz Horn <>
    escribió:
    > * holmes86 [Tue, 27 Oct 2009 00:55:36 -0700]:


    >> Is there function of modify row content in python? just like 'sed' in
    >> shell,thanks

    >
    > Sed doesn't actually modify the contents of a file. In normal operation
    > mode it reads from STDIN and writes to STDOUT. Some versions of sed
    > provide the -i option to
    >
    > edit files in place (makes backup if extension supplied)
    >
    > You could program a similar behaviour in Python using the re module.


    There is the fileinput module:
    http://docs.python.org/library/fileinput.html

    --
    Gabriel Genellina
     
    Gabriel Genellina, Oct 27, 2009
    #3
  4. holmes86

    Simon Hibbs Guest

    On 27 Oct, 07:55, holmes86 <> 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
     
    Simon Hibbs, Oct 27, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hazz
    Replies:
    6
    Views:
    49,937
    SkyUCHC
    Jun 9, 2010
  2. Replies:
    4
    Views:
    433
    Alex Martelli
    Dec 28, 2005
  3. Replies:
    2
    Views:
    845
    Jorge Godoy
    Mar 12, 2007
  4. D
    Replies:
    0
    Views:
    238
  5. Modify Table Row Span!!!

    , Oct 6, 2004, in forum: Javascript
    Replies:
    0
    Views:
    79
Loading...

Share This Page