Search code examples
pythonhtml-parser

Split string with commas also splits ampersands


The code below parses an HTML, the trouble is splitting when ampersands appear in the data.

from HTMLParser import HTMLParser

data = '<HTML><meta http-equiv="Pragma" content="no-cache"></head>'\
'<body>107,1,236,1000,70,498,NameA NameB & NameC - ActionA ActionB</body></html>'

class MyHTMLParser(HTMLParser):
      def handle_data(self, data):
            print data.split(',')

parser = MyHTMLParser()
parser.feed(data)

Output
It is splitting the '&' instead of only commas.

['107', '1', '236', '1000', '70', '498', 'NameA NameB ']
['&']
[' NameC - ActionA ActionB']

Thanks


Solution

  • Well I think this is the way to go,

    data2 = data.replace('&', 'and')