davea at ieee.org
Sat Jul 11 23:38:04 CEST 2009
Pete Froslie wrote:
> thanks for the advice Alan.. I am wondering about the following:
> new_word = response3
> old_word = response[word_number]
> #this works but adds carriage returns*********
> for line in fileinput.FileInput("journey_test.txt",inplace=1):
> line = line.replace(old_word, new_word)
> print line
> It seems that if I put it in at the difficulty location it works, but it
> also adds carriage returns to the entire text file. Basically it ends up
> double spacing a single-spaced document. I'm also not sure if this is
> causing the trouble you suggested against (writing and reading at the same
> On Sat, Jul 11, 2009 at 3:38 PM, ALAN GAULD <alan.gauld at btinternet.com>wrote:
>>> I am having trouble with probably the most simple part:
>>> I cannot seem to go back into the 'txt' file and replace the word I just
>>> searched with the new word!
>> Its not a good idea to try to read and write to the same file at the same
>> time. The normal approach is to weither ead the file into memory and
>> process it then write it back to the file or to open a second file and
>> write to that then copy the second file over the original.
>>> One with re.sub, which I can't seem to get to work
>> re.sub works on a text string it doesn't affect the file.
>> read the content into a string, close the input file.
>> Use re.sub to make the changes (or even just the replace
>> method of strings) then write the changed string back out
>> to the file.
>> Alan G
fileinput.FileInput() finesses the problem of updating "in place" very
nicely. But it also replaces STDOUT, which can make it harder to debug
your code, because if you put other print statements there, they'll go
to the file, by default. Anyway, here's your answers to the doublespacing:
If you put a trailing comma on the print statement, it won't append an
extra newline, so you won't end up doublespacing.
Or you could rstrip() the 'line' variable, to remove the newline that's
there before print.
Or you could write to stdout using write() instead of print.
I'd like to know if there's a reason you're putting this code all
inline. By breaking it into functions, you'd have a chance to work on
the different aspects of it independently. That also might mean that
you could change different aspects of it independently, or even
parameterize it from a command line to do slightly different things.
For example, what if you'd like to substitute more than one word for its
synomym? Do you really want to pass through the entire file each time?
Or what if you want to get your synomyms from somewhere else, or from
More information about the Tutor