Python Forum
Object has no attribute 'replaceall' ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Object has no attribute 'replaceall' ?
#1
I try to find all characters from the given list and replace them with one character. I`m getting "object has no attribute 'replaceall' ?

list = 'bcdfghjklmnpqrstuvwxyzBCDFGHJKLMNPQRSTUVWXYZ'
string = "aabbccddeeff"
string2 = string.replaceall(list, "!")
print(string2)

I would like to get: !!bbccdd!!ff as a and e are not on the list.
Reply
#2
There is no "str.replaceall()". There is a "str.replace()" method, but it doesn't do what you want to do.
Output:
>>> help(str.replace) Help on method_descriptor: replace(self, old, new, count=-1, /) Return a copy with all occurrences of substring old replaced by new. count Maximum number of occurrences to replace. -1 (the default value) means replace all occurrences. If the optional argument count is given, only the first count occurrences are replaced.
There is the re (regular expressions) library. It has a function named "sub()" that looks like it will do what you want.

https://docs.python.org/3/library/re.html

Search for "complementing" in the section about Regular Expression Syntax..
Reply
#3
You can do
from string import ascii_letters
spam =  'bcdfghjklmnpqrstuvwxyzBCDFGHJKLMNPQRSTUVWXYZ'

trans = str.maketrans({char:'!' for char in ascii_letters if char not in spam})
eggs = "aabbccddeeff"
foo = eggs.translate(trans)
print(foo)
Docs on str.maketrans() and str.translate()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  AttributeError: 'str' object has no attribute 'size' russoj5 4 7,488 Nov-15-2020, 11:43 PM
Last Post: deanhystad
  Calling an class attribute via a separate attribute in input wiggles 7 2,889 Apr-04-2020, 10:54 PM
Last Post: wiggles
  AttributeError: 'list' object has no attribute 'g_s' NitinL 6 3,390 Mar-31-2020, 10:24 AM
Last Post: pyzyx3qwerty
  ERROR NoneType object has no attribute content denizkb 1 2,634 Nov-21-2019, 01:18 PM
Last Post: denizkb
  AttributeError: 'tuple' object has no attribute 'move' senfik99 2 4,043 Feb-26-2019, 12:42 PM
Last Post: stullis
  text = str(text.encode('utf-8')) AttributeError: 'float' object has no attribute 'enc ulrich48155 2 8,753 Jul-31-2017, 05:21 PM
Last Post: ulrich48155

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020