[2.5.1] Read each line from txt file, replace, and save?

Discussion in 'Python' started by Gilles, Sep 2, 2012.

  1. Gilles

    Gilles Guest

    Hello

    This is a newbie question.

    I need to read a text file into a variable, loop through each line and
    use a regex to substitute some items within the line, and save the
    whole variable into a new text file.

    This triggers an error when I save the modified variable that contains
    all the lines:
    ==================
    import re,sys

    f = open("C:\\input.txt", "r")
    textlines = f.readlines()
    f.close()

    for line in textlines:
    #edit each line
    line = "just a test"

    #rewrite data to new file
    log = open('output.sub','w')
    #ERROR: argument 1 must be string or read-only character buffer, not
    list
    log.write(textlines)
    log.close()
    ==================

    Should I use another way to read the file, edit each line, and save
    the data into a new file?

    Thank you.
     
    Gilles, Sep 2, 2012
    #1
    1. Advertising

  2. Gilles

    Gilles Guest

    On Sun, 02 Sep 2012 12:19:02 +0200, Gilles <> wrote:
    (snip)

    Found it:

    #rewrite lines to new file
    output = open('output.txt','w')

    for line in textlines:
    #edit each line
    line = "just a test"
    output.write("%s" % line)

    output.close()
     
    Gilles, Sep 2, 2012
    #2
    1. Advertising

  3. On 02/09/2012 11:36, Gilles wrote:
    > On Sun, 02 Sep 2012 12:19:02 +0200, Gilles <> wrote:
    > (snip)
    >
    > Found it:
    >
    > #rewrite lines to new file
    > output = open('output.txt','w')
    >
    > for line in textlines:
    > #edit each line
    > line = "just a test"
    > output.write("%s" % line)
    >
    > output.close()
    >


    IMHO better practice to use the with statement. See
    http://docs.python.org/reference/compound_stmts.html#the-with-statement

    --
    Cheers.

    Mark Lawrence.
     
    Mark Lawrence, Sep 2, 2012
    #3
  4. Gilles

    Terry Reedy Guest

    On 9/2/2012 6:36 AM, Gilles wrote:
    > On Sun, 02 Sep 2012 12:19:02 +0200, Gilles <> wrote:
    > (snip)
    >
    > Found it:
    >
    > #rewrite lines to new file
    > output = open('output.txt','w')
    >
    > for line in textlines:
    > #edit each line
    > line = "just a test"
    > output.write("%s" % line)
    >
    > output.close()


    If you process each line separately, there is no reason to read them all
    at once. Use the file as an iterator directly. Since line is already a
    string, there is no reason to copy it into a new string. Combining these
    two changes with Mark's suggestion to use with and we have the following
    simple code:

    with open('input.txt', 'r') as inp, open('output.txt', 'w') as out:
    for line in inp:
    out.write(process(line))

    where for your example, process(line) == 'just a test\n'
    (you need explicit line ending for .write())

    --
    Terry Jan Reedy
     
    Terry Reedy, Sep 2, 2012
    #4
  5. Gilles

    Gilles Guest

    On Sun, 02 Sep 2012 14:04:29 -0400, Terry Reedy <>
    wrote:
    >If you process each line separately, there is no reason to read them all
    >at once. Use the file as an iterator directly. Since line is already a
    >string, there is no reason to copy it into a new string. Combining these
    >two changes with Mark's suggestion to use with and we have the following
    >simple code:


    Thanks guys for the suggestion.
     
    Gilles, Sep 5, 2012
    #5
    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. Sameen
    Replies:
    2
    Views:
    447
    Victor Bazarov
    Aug 29, 2005
  2. kaushikshome
    Replies:
    4
    Views:
    777
    kaushikshome
    Sep 10, 2006
  3. scad
    Replies:
    23
    Views:
    1,177
    Alf P. Steinbach
    May 17, 2009
  4. hycn office
    Replies:
    2
    Views:
    185
    WIlliam Morris
    Oct 10, 2003
  5. jobo

    How to read .txt file line by line

    jobo, Apr 17, 2007, in forum: Javascript
    Replies:
    1
    Views:
    90
    scripts.contact
    Apr 17, 2007
Loading...

Share This Page