在字符串中规范化lineends的最pythonic方法是什么?

时间:2009-11-17 15:02:47

标签: python newline line-breaks

给定一个未知来源的文本字符串,如何最好地重写它以获得已知的lineend-convention?

我通常会这样做:

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 行。

Hm,包含'\ r \ n'的混合上下文可以通过比较splitlines()和split('\ n')和/或split('\ r')...的结果来检测/ p>

3 个答案:

答案 0 :(得分:13)

mixed.replace('\r\n', '\n').replace('\r', '\n')

应该处理所有可能的变种。

答案 1 :(得分:7)

  

...但是这不会处理完全混淆的约定的“混合”文本文件(是的,它们仍然存在!)

实际上它应该可以正常工作:

>>> s = 'hello world\nline 1\r\nline 2'

>>> s.splitlines()
['hello world', 'line 1', 'line 2']

>>> '\n'.join(s.splitlines())
'hello world\nline 1\nline 2'

您使用的是哪个版本的Python?

编辑:我仍然看不到splitlines()对你不起作用:

>>> s = '''\
... First line, with LF\n\
... Second line, with CR\r\
... Third line, with CRLF\r\n\
... Two blank lines with LFs\n\
... \n\
... \n\
... Two blank lines with CRs\r\
... \r\
... \r\
... Two blank lines with CRLFs\r\n\
... \r\n\
... \r\n\
... Three blank lines with a jumble of things:\r\n\
... \r\
... \r\n\
... \n\
... End without a newline.'''

>>> s.splitlines()
['First line, with LF', 'Second line, with CR', 'Third line, with CRLF', 'Two blank lines with LFs', '', '', 'Two blank lines with CRs', '', '', 'Two blank lines with CRLFs', '', '', 'Three blank lines with a jumble of things:', '', '', '', 'End without a newline.']

>>> print '\n'.join(s.splitlines())
First line, with LF
Second line, with CR
Third line, with CRLF
Two blank lines with LFs


Two blank lines with CRs


Two blank lines with CRLFs


Three blank lines with a jumble of things:



End without a newline.

据我所知splitlines()不会将列表拆分两次或任何内容。

您可以粘贴那些给您带来麻烦的输入样本吗?

答案 2 :(得分:0)

是否还有比\r\n\\n更多的对话?如果你不想要线条,简单地替换\r\n就足够了。

only_newlines = mixed.replace('\r\n','\n')
相关问题