简体   繁体   中英

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

好吧,我认为这是要走的路,

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

另一种解决方案是,在<body>标记中获取值,然后使用Beautifulsoup或您选择的任何库使用data.split(',')进行解析。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM