Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > [2.5.1] Read each line from txt file, replace, and save?

Reply
Thread Tools

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

 
 
Gilles
Guest
Posts: n/a
 
      09-02-2012
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.
 
Reply With Quote
 
 
 
 
Gilles
Guest
Posts: n/a
 
      09-02-2012
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()
 
Reply With Quote
 
 
 
 
Mark Lawrence
Guest
Posts: n/a
 
      09-02-2012
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/com...with-statement

--
Cheers.

Mark Lawrence.

 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      09-02-2012
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

 
Reply With Quote
 
Gilles
Guest
Posts: n/a
 
      09-05-2012
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
counting how often the same word appears in a txt file...But my codeonly prints the last line entry in the txt file dgcosgrave@gmail.com Python 8 12-19-2012 06:29 PM
Diff. between FileWriter("f.txt") and OutputStreamWriter(new FileOutputStream("f.txt")) ? Jochen Brenzlinger Java 7 09-15-2011 01:23 AM
Read a file line by line and write each line to a file based on the5th byte scad C++ 23 05-17-2009 06:11 PM
How to read .txt file line by line jobo Javascript 1 04-17-2007 10:42 AM
How to read a text file line by line and remove some line kaushikshome C++ 4 09-10-2006 10:12 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57