-1

I have a list (new in this, not know how to call it) and want to remove some characters from it but for some reason the function replace() pops a TypeError: 'NoneType' object is not callable

lists = <div class="d-map" itemprop="geo" itemscope="" itemtype="http://schema.org/GeoCoordinates"><meta content="29.028856" itemprop="latitude"/><meta content="-110.955923" itemprop="longitude"/><div id="map" style="height: 230px;"></div></div>

char_remove = ["<", ">","[", "]", "=", ":", "/" ]
for char in char_remove:
    list = lists.replace(char, " ") 
MattDMo
  • 100,794
  • 21
  • 241
  • 231

2 Answers2

1

I would recommend RegEx for such a case because it can do the same without the need for a loop.

import re


lists = '<div class="d-map" itemprop="geo" itemscope="" itemtype="http://schema.org/GeoCoordinates"><meta content="29.028856" itemprop="latitude"/><meta content="-110.955923" itemprop="longitude"/><div id="map" style="height: 230px;"></div></div>'

result = re.sub(r'[<>\[\]=:\/]', '', lists)

Result:

'div class"d-map" itemprop"geo" itemscope"" itemtype"httpschema.orgGeoCoordinates"meta content"29.028856" itemprop"latitude"meta content"-110.955923" itemprop"longitude"div id"map" style"height 230px;"divdiv'
white
  • 601
  • 3
  • 8
0

Assuming your variable lists (not a great name in this context) is a string then:

lists = """lists = <div class="d-map" itemprop="geo" itemscope="" itemtype="http://schema.org/GeoCoordinates"><meta content="29.028856" itemprop="latitude"/><meta content="-110.955923" itemprop="longitude"/><div id="map" style="height: 230px;"></div></div>"""

char_remove = set('<>[]=:/') # the characters that we don't want

print(''.join(c for c in lists if c not in char_remove))
DarkKnight
  • 19,739
  • 3
  • 6
  • 22