[Tutor] Iterating over a long list with regular expressions andchanging each item?

Alan Gauld alan.gauld at btinternet.com
Mon May 4 17:15:21 CEST 2009


"Dan Liang" <danliang20 at gmail.com> wrote 

> def replaceTagging(source_name, target_name):
>       source_file = file(source_name, 'r')
>       source = source_file.read()       # not really necessary

this reads the entire file as a string

>       target_file = open(target_name, "w")
>       # replacement loop
>       for line in source:

this iterates over the characters in the string.
Remove the two source lines above and use

for line in open(source_name):

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list