[Tutor] raw string notation.

Israel Evans israel@lith.com
Fri, 13 Jul 2001 10:31:52 -0700


D-Man,   You're Beautiful!  And so are all of you other people on this list!

Funny thing..  The problem I was having last night seemed to dissapear.  I'm
not sure exactly what I did, but everything works like you said it should.


And about that from module import * thing...  I think I've seen it said that
that was a bad thing to do so many times, the image became fixed in my head
and as a result floated to the top of my consciousness as the only right and
proper thing to do!   Curse this Deviant Brain of Mine!  &>;] 

-----Original Message-----
From: D-Man [mailto:dsh8290@rit.edu]
Sent: Thursday, July 12, 2001 8:04 PM
To: [tutor]
Subject: Re: [Tutor] raw string notation.


On Thu, Jul 12, 2001 at 06:25:58PM -0700, Israel Evans wrote:
| Excellent! Thank you!
| 
| I still have troubles with backslashes however...
| 
| In the files, that I'm searching, I'd like to sometimes be able to 
| have oldword and newword contain a \ character.

This shouldn't be a problem.

| def wordswap(filename, oldword, newword):
|     import fileinput    
|     from string import *

Bad, bad, bad.  Not only is from-import-* considererd poor style when
used at the module level, inside a function it has undefined
sematnics. I think it is also a bit slower, since it has to iterate
over all the names in the string module and then create a local name
to match it.  BTW, that would happen for each invocation of the
function.  You don't want that overhead.  Instead use "import string",
then use "string.replace" instead of "replace".

|     for line in fileinput.input(filename, 1):
          print "'%s'" % oldword
          print "'%s'" % newword
          print "'%s'" % line

print is an excellent debugging tool, and it totaly cross-platform
<wink>

|         newline = replace(line, oldword, newword)
|         print newline,
| 
| It seems that if oldword and newword contain a \ then they never
| find anything.  The files I'm searching contain paths to resources.
| Sometimes instead of changing oneword of the name of the resource, 
| I want to change both one of the Directories and the name of the resource.
| 
| It seems that this will work in the IDLE interpreter when I'm working 
| with a list, but not when I'm working with a file.  i.e. when in the 
| for loop I use
| 	for line in list:
| 		newline = replace(line, oldword, newword)
| 		print newline,
| 
| Does anybody know why and what I should do about it that I'm missing?

It works in IDLE because it is supposed to work.  I haven't used the
fileinput module, but my first inclination is to make sure that
fileinput.input doesn't do any eval-ing on the text.  I'm thinking of
the difference between the builtin funcitons input() and raw_input().
Other than that, I would check the dta you have and make sure it
really is supposed to match in that context.  It may be a bug
somewhere else that causes your function to have the wrong data.  Use
print liberally, and ask "stupid" questions -- the bug is often times
a "stupid" oversight somewhere.

-D


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor