coolair
V2EX  ›  问与答

Python 如何替换一个字符串中多个连续的空格换行制表符为一个空格?

  •  
  •   coolair · Apr 1, 2017 · 3581 views
    This topic created in 3355 days ago, the information mentioned may be changed or developed.
    4 replies    2017-04-01 10:30:34 +08:00
    40huo
        1
    40huo  
       Apr 1, 2017
    正则?
    z4none
        2
    z4none  
       Apr 1, 2017 via Android
    split
    join
    LichMscy
        3
    LichMscy  
       Apr 1, 2017
    m = re.findall('[^\s]+', inputStr, re.S)
    n = ''.join(m)
    coolair
        4
    coolair  
    OP
       Apr 1, 2017
    import re
    pat = re.compile(r'\s+')
    s = ' \t foo \t bar \t '
    print pat.sub('', s)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5318 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 09:15 · PVG 17:15 · LAX 02:15 · JFK 05:15
    ♥ Do have faith in what you're doing.