2010-04-05 3 views
0

내가파이썬 2.5.2 스크립트는

function *{echo "The function starts here." 

하여 선

function *{

을 대체하고 싶습니다 *는 언제나 그렇습니다.

어떻게 파이썬에서 그렇게 할 수 있습니까?

감사

하비는

+0

는'*'정말 "무엇"인가? "{"자체를 포함 할 수 있습니까? (이것은 정규식이 얼마나 복잡해야하는지에 차이가 있습니다.) –

+0

사실입니다. 아니요, '*'는 '{'를 포함하지 않습니다. – ziiweb

답변

0

는 정규 표현식 텍스트로 대체 적용합니다. 찾고있는 모듈은 re입니다. 모든 스크립트는 "잘 코딩"하는 경우

3
re.compile(r'(^function .*{)', re.M).sub(r'\1echo "The function starts here."', s) 
+0

+1은're.M'을 사용하는 것을 기억합니다. –

1

,

import fileinput,os 
root="/path" 
path=os.path.join(root,"mydir") 
os.chdir(path) 
for file in os.listdir("."): 
    if os.path.isfile(file) and file.endswith(".txt"): # do for txt files 
     for line in fileinput.FileInput(file,inplace=1): 
      line=line.rstrip() 
      if "function" in line and "{" in line:      
       s=line.split("{") 
       s.insert(1,'{echo "The function starts here."') 
       line=' '.join(s) 
      print line 
+0

코드를 테스트 했습니까? – SilentGhost

관련 문제