【问题标题】:What's the most pythonic way of normalizing lineends in a string?规范化字符串中线端的最pythonic方法是什么?
【发布时间】:2010-12-17 11:51:24
【问题描述】:

给定一个未知来源的文本字符串,如何最好地将其重写为具有已知的 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 行。

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

【问题讨论】:

    标签: python newline line-breaks


    【解决方案1】:
    mixed.replace('\r\n', '\n').replace('\r', '\n')
    

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

    【讨论】:

      【解决方案2】:

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

      其实应该可以的:

      >>> 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() 不会将列表拆分两次或其他任何事情。

      您能粘贴一个给您带来麻烦的输入示例吗?

      【讨论】:

      • 我已经编辑了我的问题。这个(混合惯例)今天打破了一个测试用例:) 啊,我们所做的假设......我希望避免双重分裂的 O(n^2) 情况。
      • +1 这看起来很完美! @kaleissin您是说 splitlines() 不会在 all possible 换行符处拆分(无论约定如何)?我会感到惊讶……
      • len("a\nb\n\nc\nd".splitlines()) == 5 len("a\rb\r\nc\nd".splitlines()) == 4这是python 2.6.2。基本上,当它混合在一起时,我不会假设 '\r\n' 或 '\n\r' 应该被视为单个逻辑换行符。
      【解决方案3】:

      还有比\r\n\\n 更多的约定吗?如果您不想要线条,只需替换 \r\n 就足够了。

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

      【讨论】:

      • DOS/Windows:\015\012 (CRLF),Unix:\012 (LF),Mac:\015 (CR)。
      • 您可以使用 os.linesep 它将根据运行代码的操作系统选择正确的换行符。
      猜你喜欢
      • 1970-01-01
      • 2023-03-05
      • 2012-02-08
      • 1970-01-01
      • 2014-01-19
      • 1970-01-01
      • 1970-01-01
      • 2016-06-18
      • 1970-01-01
      相关资源
      最近更新 更多