Python split() 不删除分隔符
这段代码几乎满足了我的需要......
for line in all_lines:
s = line.split('>')
除了它删除了所有“>”分隔符。
那么,
<html><head>
有
['<html','<head']
没有办法使用 split() 方法,但保留分隔符,而不是删除它?
有了这些结果..
['<html>','<head>']
This code almost does what I need it to..
for line in all_lines:
s = line.split('>')
Except it removes all the '>' delimiters.
So,
<html><head>
Turns into
['<html','<head']
Is there a way to use the split() method but keep the delimiter, instead of removing it?
With these results..
['<html>','<head>']
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用拆分来解析 HTML,那么您很可能会做错,除非您正在编写针对固定且安全的内容文件的一次性脚本。如果它应该适用于任何 HTML 输入,您将如何处理类似
?
无论如何,以下内容对我有用:
If you are parsing HTML with splits, you are most likely doing it wrong, except if you are writing a one-shot script aimed at a fixed and secure content file. If it is supposed to work on any HTML input, how will you handle something like
<a title='growth > 8%' href='#something'>
?Anyway, the following works for me:
这个怎么样:
How about this:
只需将其拆分,然后为数组/列表中的每个元素(除了最后一个元素)添加一个尾随“>”到它。
Just split it, then for each element in the array/list (apart from the last one) add a trailing ">" to it.