2024-03-28 23:50:40 发布
网友
假设这是字符串:
The fox jumped over the log.
这将导致:
最简单的1-2行程序是什么?不分清单。。。
>>> import re >>> re.sub(' +', ' ', 'The quick brown fox') 'The quick brown fox'
foo是你的字符串:
" ".join(foo.split())
请注意,这会删除“所有空白字符(空格、制表符、换行符、回车符、formfeed)”。(感谢hhsaffar,请参阅注释)ie "this is \t a test\n"将有效地结束为"this is a test"
"this is \t a test\n"
"this is a test"
import re s = "The fox jumped over the log." re.sub("\s\s+" , " ", s)
或者
re.sub("\s\s+", " ", s)
因为逗号前的空格在PEP8中被列为pet peeve,正如moose在评论中提到的那样。
foo是你的字符串:
请注意,这会删除“所有空白字符(空格、制表符、换行符、回车符、formfeed)”。(感谢hhsaffar,请参阅注释)ie
"this is \t a test\n"
将有效地结束为"this is a test"
或者
因为逗号前的空格在PEP8中被列为pet peeve,正如moose在评论中提到的那样。
相关问题 更多 >
编程相关推荐