由于某种原因,我无法从字符串中删除竖线。该字符串是从html标记(BeautifulSoup 4)中提取的。
该字符串来自网站的标题元标记。
输入示例:“如何快速运行| running.com'
通过if语句,该字符串仍包含|,但名称中未检测到该字符串,并且从不输入if语句...
name = title.text.encode('ascii', 'ignore').strip()
#remove everything after | because often it is SEO stuff
name = re.sub('\|', '', name) #fails
if "|" in name:
lineIndex = name.index('|')
name = name[:lineIndex]
您需要将正则表达式视为原始字符串:
s = 'How to run very fast | running.com'
s = re.sub(r'\|', '', s)
>>> print s
How to run very fast running.com
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句