+ -
当前位置:首页 → 问答吧 → How would you do the equivalent in Python?

How would you do the equivalent in Python?

时间:2011-03-26

来源:互联网

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

作者: mjus   发布时间: 2011-03-26

本帖最后由 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)
复制代码

作者: llbgurs   发布时间: 2011-03-26