如何用换行符和冒号解析一行?
我有类似 this 的文本。我想按 New Line
和 Colons
进行分割。现在我正在按 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你的输出怎么奇怪?我的意思是,有很多表格
\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.str.split() 方法仅接受单个拆分器。这可以防止在冒号或换行符上进行分割。您可以通过以下方法克服此限制:
您还可以尝试 re.split() 函数可以更细粒度地控制分割:
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:
You can also try the re.split() function for more fine grained control over splitting:
这应该可以解决问题
编辑:西蒙的答案将产生相同的结果,同时成为“更干净”的解决方案。
This should do the trick
EDIT: Simon's answer will produce the same result while being a "cleaner" solution.
\t 是制表符。要清理一些内容,请尝试以下操作:
它将删除空行并去除空格
\t are tabs. To clean things a bit, try this:
it will remove empty lines and strip whitespaces
它正在执行您所要求的操作,但它充满了选项卡。通过
strip()
删除选项卡:您应该会看到更像您所期望的内容。
这仍然会留下一些空行,所以也许你也想删除这些空行:
It's doing just what you're asking, but it's full of tabs. Remove the tabs via
strip()
: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: