[Tutor] thesaurus

Pete Froslie froslie at gmail.com
Sun Jul 12 00:16:04 CEST 2009


the trailing comma is getting me much closer right away! -- will read about
the other options you suggest (rstrip(); stdout: write()) I clearly have a
few other issues I need to sort out now. i guess I've been assuming that
print was a good way to trace what is happening rather than thinking about
its results in the external file as well..

As for the inline: simply put, I'm an artist trying to learn how to code for
my work, so my decisions are not altogether as informed as they could be.
Thanks for taking the time to make that suggestion.. I will try to rework it
to make use of functions. Then I will address the next set of issues..

This seems the basic form for a function:

*def** hello*():
>     *print* "Hello World!"
>     *return*
>
> I assume each part that can be separated will be done so in this format at
the start of the code; consequently, allowing me to call 'hello' later when
I need it. Does this also mean that I will be able to call those functions
separately later when I import 'thesaurus.py' into a new code also?

Pete F

On Sat, Jul 11, 2009 at 5:38 PM, Dave Angel <davea at ieee.org> wrote:

> Pete Froslie wrote:
>
>> thanks for the advice Alan.. I am wondering about the following:
>>
>> new_word = response3[2]
>> 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
>> time).
>>
>> cheers
>>
>> 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.
>>>
>>> HTH,
>>>
>>> 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 multiple
> places?
>
> DaveA
>
>


-- 
Pete Froslie
617.314.0957
http://www.froslie.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090711/2e538a92/attachment-0001.htm>


More information about the Tutor mailing list