gpt4 book ai didi

python - 为什么替换多个连续的空格也会删除 Python 中的换行符?

转载 作者:太空宇宙 更新时间:2023-11-04 10:48:43 24 4
gpt4 key购买 nike

这是我的代码:

print apps_version
apps_version = re.sub("\s+"," ",apps_version)
print apps_version
apps_version = apps_version.splitlines()

这就是发生的事情:

backend    10.2.25
server 12.6.4.6
backend 10.2.25 server 12.6.4.6

它确实用单个空格替换了空格,但它也从行尾删除了新行符号。我通过用 | 替换空格来解决它字符并使用它来拆分行,但有人可以解释为什么替换空格会删除换行符吗?提前致谢。

最佳答案

引用documentation :

\s

When the UNICODE flag is not specified, it matches any whitespace character, this is equivalent to the set [ \t\n\r\f\v]. The LOCALE flag has no extra effect on matching of the space. If UNICODE is set, this will match the characters [ \t\n\r\f\v] plus whatever is classified as space in the Unicode character properties database.

\s 不仅匹配空格 (ASCII 32),它还会匹配水平制表符、换行符回车符、表单提要和垂直标签。如果启用 UNICODE 标志,甚至更多

如果您只关心空间,那么 r' +' 就是您所需要的。

关于python - 为什么替换多个连续的空格也会删除 Python 中的换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15519615/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com