简体   繁体   English

用逗号分隔字符串也会分隔&符号

[英]Split string with commas also splits ampersands

The code below parses an HTML, the trouble is splitting when ampersands appear in the data. 下面的代码解析HTML,当数据中出现“&”号时,麻烦就解决了。

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(',')进行解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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