Python split() 不删除分隔符

发布于 2024-12-11 14:02:20 字数 368 浏览 0 评论 0原文

这段代码几乎满足了我的需要......

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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝眼泪 2024-12-18 14:02:20
d = ">"
for line in all_lines:
    s =  [e+d for e in line.split(d) if e]
d = ">"
for line in all_lines:
    s =  [e+d for e in line.split(d) if e]
池木 2024-12-18 14:02:20

如果您使用拆分来解析 HTML,那么您很可能会做错,除非您正在编写针对固定且安全的内容文件的一次性脚本。如果它应该适用于任何 HTML 输入,您将如何处理类似 ?

无论如何,以下内容对我有用:

>>> import re
>>> re.split('(<[^>]*>)', '<body><table><tr><td>')[1::2]
['<body>', '<table>', '<tr>', '<td>']

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:

>>> import re
>>> re.split('(<[^>]*>)', '<body><table><tr><td>')[1::2]
['<body>', '<table>', '<tr>', '<td>']
陌路终见情 2024-12-18 14:02:20

这个怎么样:

import re
s = '<html><head>'
re.findall('[^>]+>', s)

How about this:

import re
s = '<html><head>'
re.findall('[^>]+>', s)
睡美人的小仙女 2024-12-18 14:02:20

只需将其拆分,然后为数组/列表中的每个元素(除了最后一个元素)添加一个尾随“>”到它。

Just split it, then for each element in the array/list (apart from the last one) add a trailing ">" to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文