简体   繁体   English

字符串连接与列表中的空字符串

[英]String join with empty string in list

Short context: I have a Python application that has a long string with mixed line endings: \n and \r\n .简短的上下文:我有一个 Python 应用程序,它有一个带有混合行结尾的长字符串: \n\r\n I'm trying to convert the line endings to Windows line endings \r\n with the following code:我正在尝试使用以下代码将行尾转换为 Windows 行尾\r\n

s = "one\r\ntwo\r\n\r\nthree\n\nfour\nfive\r\n\r\n"
lines = s.splitlines()
print(lines)
out = '\r\n'.join(lines)
print(repr(out))
print(out, end='')

The output is: output 是:

['one', 'two', '', 'three', '', 'four', 'five', '']
'one\r\ntwo\r\n\r\nthree\r\n\r\nfour\r\nfive\r\n'
one
two

three

four
five

The only caveat is that last empty string, I'd like it to generate an empty line for it, like the other empty lines in the input (like the one between 'two' and 'three').唯一需要注意的是最后一个空字符串,我希望它为它生成一个空行,就像输入中的其他空行(如“二”和“三”之间的空行)。

It's curious that if I change my input string to奇怪的是,如果我将输入字符串更改为

s = "one\r\ntwo\r\n\r\nthree\n\nfour\nfive\r\n\r\n\r\n"

The output is: output 是:

['one', 'two', '', 'three', '', 'four', 'five', '', '']
'one\r\ntwo\r\n\r\nthree\r\n\r\nfour\r\nfive\r\n\r\n'
one
two

three

four
five

So the problem is that join() seems to be inconsistent by skipping that last empty string.所以问题是join()通过跳过最后一个空字符串似乎不一致。

Long context: This application reads some XML files with LXML, and those files have the Unix termination \n .长上下文:此应用程序使用 LXML 读取一些 XML 文件,这些文件具有 Unix 终止\n I do some processing with them that creates strings that have the Windows termination.我对它们进行了一些处理,以创建具有 Windows 终止的字符串。 So then they're put together, I get this string with mixed termination.然后将它们放在一起,我得到这个带有混合终止的字符串。

You might use a pattern to match an optional \r followed by \n and use \r\n in the replacement.您可以使用模式来匹配可选的\r后跟\n并在替换中使用\r\n

import re
s = "one\r\ntwo\r\n\r\nthree\n\nfour\nfive\r\n\r\n"
s = re.sub(r"\r?\n", r"\r\n", s)
print(s)
one
two

three

four
five


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM