推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
kenneth
V2EX  ›  Python

请教一个字符串分割的问题

  •  
  •   kenneth · Nov 28, 2014 · 2816 views
    This topic created in 4359 days ago, the information mentioned may be changed or developed.
    例如
    url='http://www.test.com/a=1&b=2&c=3'
    print url.split('&')[0]

    我希望得到
    url='http://www.test.com/a=1&b=2

    也就是按最后一个&分
    要怎么写?
    3 replies  •  2014-12-02 02:35:21 +08:00
    whywhywhy
    whywhywhy
    Nov 29, 2014
    1
    url='http://www.test.com/a=1&b=2&c=3'
    print url.split('&c=')[0]
    irainy
    irainy
    Nov 29, 2014
    2
    '&'.join(url.split('&')[:-1])
    KentY
    KentY
    Dec 2, 2014 via iPhone
    3
    手机cp/paste不方便,你就re.sub(u'&[^&]*$','',url)就好了吧
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1392 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 20:50 · PVG 04:50 · LAX 12:50 · JFK 15:50
    ♥ Do have faith in what you're doing.