V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2014-11-28 23:52:34 +08:00 · 1946 次点击
    这是一个创建于 3429 天前的主题,其中的信息可能已经有所发展或是发生改变。
    例如
    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 条回复    2014-12-02 02:35:21 +08:00
    whywhywhy
        1
    whywhywhy  
       2014-11-29 00:51:22 +08:00
    url='http://www.test.com/a=1&b=2&c=3'
    print url.split('&c=')[0]
    irainy
        2
    irainy  
       2014-11-29 01:30:35 +08:00
    '&'.join(url.split('&')[:-1])
    KentY
        3
    KentY  
       2014-12-02 02:35:21 +08:00 via iPhone
    手机cp/paste不方便,你就re.sub(u'&[^&]*$','',url)就好了吧
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3657 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 00:11 · PVG 08:11 · LAX 17:11 · JFK 20:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.