亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2498 | 回復: 7
打印 上一主題 下一主題

How would you do the equivalent in Python? [復制鏈接]

論壇徽章:
0
跳轉到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2011-03-26 05:15 |只看該作者 |倒序瀏覽
In perl, below code snippet can print out the data between BEGIN ~ END from a text file.

while(<IN> {
     print if (/#BEGIN/oi .. /#END/oi);
  }

I wonder : How would you do the equivalent in Python? Thank you in advance !!!

BTW: the perl code at least runs V5.8.8

論壇徽章:
0
2 [報告]
發(fā)表于 2011-03-26 13:09 |只看該作者
本帖最后由 llbgurs 于 2011-03-26 13:19 編輯

try this:
  1. import re

  2. f =open("data")
  3. pat = re.compile(r"#BEGIN(.*?)#END", re.I)

  4. for line in f:
  5.     match_obj = re.search(pat, line)
  6.     if match_obj is not None:
  7.         print match_obj.group(1)

復制代碼

論壇徽章:
0
3 [報告]
發(fā)表于 2011-03-26 21:46 |只看該作者
回復 2# llbgurs

Given data file as follows:

# start data test file

#BEGIN
line 1
line 2
line 3
line 4
line 5
#END

# end of data test file

then kicked off your script, it didn't output anything matched. I'm new to python, think your pattern looks ok from the point of my perl view, I guess that you forgot to turn on multiple line mode, is it ? anyway, thank you very much for your kind and quick reply.

論壇徽章:
0
4 [報告]
發(fā)表于 2011-03-26 23:33 |只看該作者

  1. import sys, itertools
  2. sys.stdout.writelines(itertools.takewhile('#END\n'.__ne__,itertools.dropwhile('#BEGIN\n'.__ne__, sys.stdin)))
復制代碼

論壇徽章:
0
5 [報告]
發(fā)表于 2011-03-26 23:36 |只看該作者
  1. import re

  2. f =open("data")
  3. pat = re.compile(r"#BEGIN(.*?)#END", re.I|re.S)

  4. text = f.read()

  5. match_obj = pat.search(text)
  6. if match_obj is not None:
  7.         print match_obj.group()

  8. f.close()
復制代碼
This should be work, if no require to print #BEGIN and $END

try print match_obj.group(1)

論壇徽章:
0
6 [報告]
發(fā)表于 2011-03-27 01:50 |只看該作者
回復 5# llbgurs


    It is a perfect solution !!! Thanks a lot !!!

論壇徽章:
0
7 [報告]
發(fā)表于 2011-03-27 01:58 |只看該作者
Kabie 發(fā)表于 2011-03-26 23:33



    It looks coolest ! although it printed out below
---------------
#BEGIN
line 1
........
line 5
---------------
    It seemed not print out #END  -- it is a bug ? could you take care of it ? thank you so much !!!

論壇徽章:
0
8 [報告]
發(fā)表于 2011-03-27 08:10 |只看該作者
I guess you have to print '#END' manually afterwards...
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術有限公司. 版權所有 京ICP備16024965號-6 北京市公安局海淀分局網監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關心和支持過ChinaUnix的朋友們 轉載本站內容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP