coolair
V2EX  ›  问与答

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

  •  
  •   coolair · Apr 1, 2017 · 3585 views
    This topic created in 3358 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   ·   5344 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 787ms · UTC 09:05 · PVG 17:05 · LAX 02:05 · JFK 05:05
    ♥ Do have faith in what you're doing.