Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28259 > unrolled thread

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

Started byGilles <nospam@nospam.com>
First post2012-09-02 12:19 +0200
Last post2012-09-05 14:52 +0200
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  [2.5.1] Read each line from txt file, replace, and save? Gilles <nospam@nospam.com> - 2012-09-02 12:19 +0200
    Re: [2.5.1] Read each line from txt file, replace, and save? Gilles <nospam@nospam.com> - 2012-09-02 12:36 +0200
      Re: [2.5.1] Read each line from txt file, replace, and save? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-09-02 12:02 +0100
      Re: [2.5.1] Read each line from txt file, replace, and save? Terry Reedy <tjreedy@udel.edu> - 2012-09-02 14:04 -0400
        Re: [2.5.1] Read each line from txt file, replace, and save? Gilles <nospam@nospam.com> - 2012-09-05 14:52 +0200

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

FromGilles <nospam@nospam.com>
Date2012-09-02 12:19 +0200
Subject[2.5.1] Read each line from txt file, replace, and save?
Message-ID<uec64893qomnns4ih36319m47bkkpqr488@4ax.com>
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.

[toc] | [next] | [standalone]


#28261

FromGilles <nospam@nospam.com>
Date2012-09-02 12:36 +0200
Message-ID<nid648l7k80oli3t84okl598s5fqkgascn@4ax.com>
In reply to#28259
On Sun, 02 Sep 2012 12:19:02 +0200, Gilles <nospam@nospam.com> 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()

[toc] | [prev] | [next] | [standalone]


#28262

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2012-09-02 12:02 +0100
Message-ID<mailman.77.1346583562.27098.python-list@python.org>
In reply to#28261
On 02/09/2012 11:36, Gilles wrote:
> On Sun, 02 Sep 2012 12:19:02 +0200, Gilles <nospam@nospam.com> 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.

[toc] | [prev] | [next] | [standalone]


#28285

FromTerry Reedy <tjreedy@udel.edu>
Date2012-09-02 14:04 -0400
Message-ID<mailman.97.1346609082.27098.python-list@python.org>
In reply to#28261
On 9/2/2012 6:36 AM, Gilles wrote:
> On Sun, 02 Sep 2012 12:19:02 +0200, Gilles <nospam@nospam.com> 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

[toc] | [prev] | [next] | [standalone]


#28482

FromGilles <nospam@nospam.com>
Date2012-09-05 14:52 +0200
Message-ID<anie489odk5jaqi5faurj615f8lirk97qv@4ax.com>
In reply to#28285
On Sun, 02 Sep 2012 14:04:29 -0400, Terry Reedy <tjreedy@udel.edu>
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web