2 Replies - 6229 Views - Last Post: 01 March 2010 - 11:06 PM Rate Topic: -----

#1 RoseWyvern  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 17
  • Joined: 29-December 09

Python problem with str.replace() and str.strip()

Posted 01 March 2010 - 04:41 PM

I'm trying to use str.replace() to remove unwanted characters from a file. For whatever reason, str.strip() does nothing (it runs without error though.)

The problem is that there are several different sets of characters I want to remove. So I created a tuple, but I can't find a way to get str.replace() to iterate through it.

I've tried the following.

the vars are as follows:

Replace is the whitespace character I want to convert the other characters too.

Remove is a tuple containing the character sequences I want to remove.

Test is a string with the file data.

 
map(test.replace,remove)
#TypeError: too few arguments
map(test.replace,remove, replace)
#ditto: expected a character buffer object 
test.replace(remove, "     ")
#ditto:ditto
map(test.replace(remove,"   "),range(len(remove)))
#ditto:ditto
map(replace(remove,"   "),test)
#Strings object is not callable 




I'm using " " as my replace char mainly to check if the function had worked.

So, anyone care to point me in the right direction on str.replace() or explain why str.strip() is doing nothing?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Python problem with str.replace() and str.strip()

#2 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: Python problem with str.replace() and str.strip()

Posted 01 March 2010 - 04:54 PM

remove = "./%"
test = "foo.bar/baz%%%"
for r in remove:
    test = test.replace(r, "")
# test should now be "foobarbaz"


Was This Post Helpful? 0
  • +
  • -

#3 RoseWyvern  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 17
  • Joined: 29-December 09

Re: Python problem with str.replace() and str.strip()

Posted 01 March 2010 - 11:06 PM

Thanks! This was just what I was looking for.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1