如何用换行符和冒号解析一行?

发布于 2024-12-16 19:49:30 字数 303 浏览 0 评论 0原文

我有类似 this 的文本。我想按 New LineColons 进行分割。现在我正在按 New Lines 进行拆分。我正在使用此代码。

`data= data.split('\n')

它给出了奇怪的输出。你能帮忙吗?谢谢

I have text like this. I want to split by New Line and Colons. Right now I'm splitting by New Lines.I'm using this code.

`data= data.split('\n')

and Its giving strange output. Can you please help? thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

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

评论(5

灯角 2024-12-23 19:49:31

你的输出怎么奇怪?我的意思是,有很多表格 \t 和空格,但是您能解释一下什么不适合您的需要吗?

也许您可以先使用 .strip() 行:这将删除空格。

How is your output strange? I mean, there's lots of tabulations \t and white spaces, but could you explain what does not suit your need?

Maybe you could .strip() lines first: this will remove blank spaces.

忆沫 2024-12-23 19:49:31

str.split() 方法仅接受单个拆分器。这可以防止在冒号或换行符上进行分割。您可以通过以下方法克服此限制:

data = data.replace(':', '\n').split('\n')   # split on newlines and colons

您还可以尝试 re.split() 函数可以更细粒度地控制分割:

>>> import re
>>> s = 'abc:def\nghi'
>>> re.split(r'\:|\n', s)
['abc', 'def', 'ghi']

The str.split() method only accepts a single splitter. This precludes splitting on either a colon or a newline. You can overcome this limitation with something like:

data = data.replace(':', '\n').split('\n')   # split on newlines and colons

You can also try the re.split() function for more fine grained control over splitting:

>>> import re
>>> s = 'abc:def\nghi'
>>> re.split(r'\:|\n', s)
['abc', 'def', 'ghi']
慕巷 2024-12-23 19:49:31

这应该可以解决问题

data = list(
    line.strip().split(":") for line in data.split("\n")
)

data = filter(None, data)

编辑:西蒙的答案将产生相同的结果,同时成为“更干净”的解决方案。

data = list(
    line.strip().split(":") for line in data.split("\n") if line.strip()
)

This should do the trick

data = list(
    line.strip().split(":") for line in data.split("\n")
)

data = filter(None, data)

EDIT: Simon's answer will produce the same result while being a "cleaner" solution.

data = list(
    line.strip().split(":") for line in data.split("\n") if line.strip()
)
煮酒 2024-12-23 19:49:30

\t 是制表符。要清理一些内容,请尝试以下操作:

data = [line.strip().split(':') for line in data.split('\n') if line.strip()]

它将删除空行并去除空格

\t are tabs. To clean things a bit, try this:

data = [line.strip().split(':') for line in data.split('\n') if line.strip()]

it will remove empty lines and strip whitespaces

微暖i 2024-12-23 19:49:30

它正在执行您所要求的操作,但它充满了选项卡。通过 strip() 删除选项卡:

lines = data.split('\n')
lines = [line.strip() for line in lines]

您应该会看到更像您所期望的内容。

这仍然会留下一些空行,所以也许你也想删除这些空行:

lines = filter(None, lines)

It's doing just what you're asking, but it's full of tabs. Remove the tabs via strip():

lines = data.split('\n')
lines = [line.strip() for line in lines]

and you should see something more like what you're expecting.

That will still leave some blank lines, so maybe you'll want to strip those too:

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