Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #38864 > unrolled thread
| Started by | python <mailtomanage@163.com> |
|---|---|
| First post | 2013-02-14 22:13 +0800 |
| Last post | 2013-02-14 22:13 +0800 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
how to right the regular expression ? python <mailtomanage@163.com> - 2013-02-14 22:13 +0800
| From | python <mailtomanage@163.com> |
|---|---|
| Date | 2013-02-14 22:13 +0800 |
| Subject | how to right the regular expression ? |
| Message-ID | <mailman.1764.1360852148.2939.python-list@python.org> |
[Multipart message — attachments visible in raw view] — view raw
my tv.txt is :
http://202.177.192.119/radio5 香港电台第五台(可于Totem/VLC/MPlayer播放)
http://202.177.192.119/radio35 香港电台第五台(DAB版,可于Totem/VLC/MPlayer播放)
http://202.177.192.119/radiopth 香港电台普通话台(可于Totem/VLC/MPlayer播放)
http://202.177.192.119/radio31 香港电台普通话台(DAB版,可于Totem/VLC/MPlayer播放)
octoshape:rthk.ch1 香港电台第一台(粤)
octoshape:rthk.ch2 香港电台第二台(粤)
octoshape:rthk.ch6 香港电台普通话台
octoshape:rthk.ch3 香港电台第三台(英)
what i want to get the result is
1group is http://202.177.192.119/radio5 2group is 香港电台第五台 3group is (可于Totem/VLC/MPlayer播放)
1group is http://202.177.192.119/radio35 2group is 香港电台第五台 3group is (DAB版,可于Totem/VLC/MPlayer播放)
1group is http://202.177.192.119/radiopth 2group is 香港电台普通话台 3group is (可于Totem/VLC/MPlayer播放)
1group is http://202.177.192.119/radio31 2group is 香港电台普通话台 3group is (DAB版,可于Totem/VLC/MPlayer播放)
1group is octoshape:rthk.ch1 2group is 香港电台第一台 3group is (粤)
1group is octoshape:rthk.ch2 2group is 香港电台第二台 3group is (粤)
1group is octoshape:rthk.ch6 2group is 香港电台普通话台 3group is none
1group is octoshape:rthk.ch3 2group is 香港电台第三台 3group is (英)
here is my code:
# -*- coding: utf-8 -*-
import re
rfile=open("tv.txt","r")
pat='([a-z].+?\s)(.+)(\(.+\))'
for line in rfile.readlines():
Match=re.match(pat,line)
print "1group is ",Match.group(1),"2group is ",Match.group(2),"3group is ",Match.group(3)
rfile.close()
the output is :
1group is http://202.177.192.119/radio5 2group is 香港电台第五台 3group is (可于Totem/VLC/MPlayer播放)
1group is http://202.177.192.119/radio35 2group is 香港电台第五台 3group is (DAB版,可于Totem/VLC/MPlayer播放)
1group is http://202.177.192.119/radiopth 2group is 香港电台普通话台 3group is (可于Totem/VLC/MPlayer播放)
1group is http://202.177.192.119/radio31 2group is 香港电台普通话台 3group is (DAB版,可于Totem/VLC/MPlayer播放)
1group is octoshape:rthk.ch1 2group is 香港电台第一台 3group is (粤)
1group is octoshape:rthk.ch2 2group is 香港电台第二台 3group is (粤)
1group is
Traceback (most recent call last):
File "tv.py", line 7, in <module>
print "1group is ",Match.group(1),"2group is ",Match.group(2),"3group is ",Match.group(3)
AttributeError: 'NoneType' object has no attribute 'group'
how to revise my code to get the output?
Back to top | Article view | comp.lang.python
csiph-web