[Tutor] wierd replace problem

Michael Powe michael at trollope.org
Mon Sep 13 15:25:49 CEST 2010


In your script, you need to escape each backslash, in order to have it
come out correctly to the interpreter.  IOW, in the shell, '\\' is
what is being processed.  But in your script, you want to send '\\' to
the shell, and in order to do that, you have to escape each backslash,
or '\\\\'.  

In a script, '\\' means 'send through a backslash' for shell
processing. The left-most backslash is escaping the one to its right. 



On Mon, Sep 13, 2010 at 12:19:23PM +0000, Roelof Wobben wrote:
> Hello, 
> I have this string called test with the contents of 'het is een wonder \\TIS'
> Now I want to get rid of the \\ so I do this : test2 = test.replace ('\\', '')
> And I get at the python prompt this answer : 'het is een wonder TIS' 
> So that's right.
> Now I try the same in a IDE with this programm :
> woorden =[]
> letter_counts = {}
> file = open ('alice_in_wonderland.txt', 'r')
> for line in file:
>     line2 = line.replace ("\\","")
>     line3 = line2.lower()
>     woorden = line3.split()
>     for letter in woorden:
>         letter_counts[letter] = letter_counts.get (letter, 0) + 1
> letter_items = letter_counts.items()
> letter_items.sort()
> print letter_items 
> But now Im gettting this output :
> [('"\'tis', 1), 
> Why does the \ stays here. It should have gone as the test in the python prompt says.
> Roelof
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

Michael Powe		michael at trollope.org		Naugatuck CT USA
"...we built a new continent in cyberspace where we could go live and be
free. And the nice thing is that, because we built it, we didn't have
to steal it from aboriginal peoples. It was completely empty, and we
invite everyone to live there with us. No immigration restrictions.
There's room for everyone in the world. Come live in the free world
and be free. That's our idea."  -- Richard Stallman
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/tutor/attachments/20100913/afc454a1/attachment.pgp>

More information about the Tutor mailing list