标准化字符串中的 lineends 的最 Pythonic 方法是什么?
给定一个来源未知的文本字符串,如何最好地重写它以具有已知的行尾约定?
我通常这样做:
lines = text.splitlines()
text = '\n'.join(lines)
...但这不能处理完全混乱的约定的“混合”文本文件(是的,它们仍然存在!)。
编辑
我正在做的事情的重点当然是:
'\n'.join(text.splitlines())
......这不是我要问的。
之后的总行数应该相同,因此不会删除空行。
测试用例
分割
'a\nb\n\nc\nd'
'a\r\nb\r\n\r\nc\r\nd'
'a\rb\r\rc\rd'
'a\rb\n\rc\rd'
'a\rb\r\nc\nd'
'a\nb\r\nc\rd'
..应该全部产生5行。在混合上下文中,分割线假定 '\r\n' 是单个逻辑换行符,导致最后两个测试用例有 4 行。
嗯,可以通过比较 splitlines() 和 split('\n') 和/或 split('\r') 的结果来检测包含 '\r\n' 的混合上下文...
Given a text-string of unknown source, how does one best rewrite it to have a known lineend-convention?
I usually do:
lines = text.splitlines()
text = '\n'.join(lines)
... but this doesn't handle "mixed" text-files of utterly confused conventions (Yes, they still exist!).
Edit
The oneliner of what I'm doing is of course:
'\n'.join(text.splitlines())
... that's not what I'm asking about.
The total number of lines should be the same afterwards, so no stripping of empty lines.
Testcases
Splitting
'a\nb\n\nc\nd'
'a\r\nb\r\n\r\nc\r\nd'
'a\rb\r\rc\rd'
'a\rb\n\rc\rd'
'a\rb\r\nc\nd'
'a\nb\r\nc\rd'
.. should all result in 5 lines. In a mixed context, splitlines assumes that '\r\n' is a single logical newline, leading to 4 lines for the last two testcases.
Hm, a mixed context that contains '\r\n' can be detected by comparing the result of splitlines() and split('\n'), and/or split('\r')...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
应该处理所有可能的变体。
should handle all possible variants.
实际上它应该可以正常工作:
您使用的是哪个版本的Python?
编辑:我仍然不明白
splitlines()
为什么不适合你:据我所知
splitlines()
不适合你将列表拆分两次或其他任何内容。您能否粘贴给您带来麻烦的输入类型的示例?
Actually it should work fine:
What version of Python are you using?
EDIT: I still don't see how
splitlines()
is not working for you:As far as I know
splitlines()
doesn't split the list twice or anything.Can you paste a sample of the kind of input that's giving you trouble?
还有比
\r\n\
和\n
更多的约定吗?如果您不需要线条,只需替换\r\n
就足够了。Are there even more convetions than
\r\n\
and\n
? Simply replacing\r\n
is enough if you dont want lines.