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
xiaowangge
V2EX  ›  Python

Python 练习册,每天一个小程序 :-)

  •  1
     
  •   xiaowangge ·
    Yixiaohan · 2014-12-16 18:44:19 +08:00 · 6788 次点击
    这是一个创建于 3417 天前的主题,其中的信息可能已经有所发展或是发生改变。
    欢迎大家 Pull Request 出题目,贴代码(Gist、Blog皆可):-)

    题目:
    https://github.com/Yixiaohan/show-me-the-code

    参考代码:
    https://github.com/Show-Me-the-Code/python
    11 条回复    2015-01-11 20:28:59 +08:00
    jookr
        1
    jookr  
       2014-12-16 19:14:29 +08:00
    大神 能否指点一下,我昨天刚入门
    正则截取内容不会
    下面这个例子 我需要截取"啊啊啊"这个内容出来怎么获取不到呢

    import re
    html = 'www啊啊啊,呵呵,哈哈,嘿嘿'
    counts = re.findall(r'"www(.*),呵呵,哈哈,嘿嘿', html)

    print counts[1]

    raw_input()
    realityone
        2
    realityone  
       2014-12-16 19:28:02 +08:00   ❤️ 1
    @jookr 刚入门哪有这么跳内容的。。
    counts = re.findall(r'www(.*)', html)
    正则入门吧。。
    jookr
        3
    jookr  
       2014-12-16 19:30:35 +08:00
    中文的逗号匹配结尾会提示错误怎么办
    jookr
        4
    jookr  
       2014-12-16 19:40:30 +08:00
    @realityone 谢谢
    但是你的代码会获取整个www后面的内容,不能只截取出"啊啊啊"

    import re
    html = 'www啊啊啊,呵呵,哈哈,嘿嘿'
    #counts = re.findall(r'"www(.*),呵呵,哈哈,嘿嘿', html)
    counts = re.findall(r'www(.*)', html)
    get_content = counts[0]

    print get_content.decode('utf-8').encode('cp936')
    jookr
        5
    jookr  
       2014-12-16 19:50:52 +08:00
    自己解决了


    # -*- coding: utf-8 -*-


    import re
    html = 'www啊啊啊,呵呵,哈哈,嘿嘿'
    #counts = re.findall(r'"www(.*),呵呵,哈哈,嘿嘿', html)
    counts = re.findall(r'www(.*?),(.*?),(.*?),', html)
    get_content = counts[0]

    #print get_content

    for i in get_content:
    print i.decode('utf-8').encode('cp936')
    Bitex
        6
    Bitex  
       2014-12-16 19:58:10 +08:00
    @jookr

    count = re.findall(r'www(.*?),', html)

    既然已经用了非贪婪匹配,就没必要把后面的全加上了
    Bitex
        7
    Bitex  
       2014-12-16 20:03:07 +08:00
    @jookr

    count = re.findall(r'www([^,]*?),', html)

    应该是这样……上面打错了。

    没用过Python3,不知道[^,]这种写法有没有问题
    0x5e
        8
    0x5e  
       2014-12-16 21:52:42 +08:00
    @realityone 哟西,好巧:-)
    GeekGao
        9
    GeekGao  
       2014-12-17 02:11:21 +08:00
    fun
    4everLoveU
        10
    4everLoveU  
       2014-12-19 11:12:56 +08:00
    好东西,不错,支持一下!
    mingyun
        11
    mingyun  
       2015-01-11 20:28:59 +08:00
    star
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3987 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 05:10 · PVG 13:10 · LAX 22:10 · JFK 01:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.