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

2014-12-16 18:44:19 +08:00
 xiaowangge
欢迎大家 Pull Request 出题目,贴代码(Gist、Blog皆可):-)

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

参考代码:
https://github.com/Show-Me-the-Code/python
6791 次点击
所在节点    Python
11 条回复
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
2014-12-16 19:28:02 +08:00
@jookr 刚入门哪有这么跳内容的。。
counts = re.findall(r'www(.*)', html)
正则入门吧。。
jookr
2014-12-16 19:30:35 +08:00
中文的逗号匹配结尾会提示错误怎么办
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
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
2014-12-16 19:58:10 +08:00
@jookr

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

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

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

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

没用过Python3,不知道[^,]这种写法有没有问题
0x5e
2014-12-16 21:52:42 +08:00
@realityone 哟西,好巧:-)
GeekGao
2014-12-17 02:11:21 +08:00
fun
4everLoveU
2014-12-19 11:12:56 +08:00
好东西,不错,支持一下!
mingyun
2015-01-11 20:28:59 +08:00
star

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/154410

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX