coolair
V2EX  ›  问与答

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

  •  
  •   coolair · Apr 1, 2017 · 3618 views
    This topic created in 3378 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   ·   944 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 60ms · UTC 20:30 · PVG 04:30 · LAX 13:30 · JFK 16:30
    ♥ Do have faith in what you're doing.