기본적으로 텍스트 파일이 있고 문장의 중간 단어를 검색하려고합니다. 내 .py
스크립트를 실행할 때 found_state not defined
이라고 말하는 오류가 발생합니다. 텍스트 파일의 중간에 특정 단어를 찾는 Python 정규식
if re.search('(?<=#)\w+',data):
다음 found_state이 선언되지 않은, 실패하면
import re;
import time;
with open('file.conf') as f:
content = f.readlines()
name=''
for data in content:
if re.search('(?<=#)\w+',data):
found_state=1
name=data
break
if found_state==1:
print name + "is Found"
else:
print "NF"
그래서 'found_state'를 미리 선언하십시오. 스크립트는 어디서 사용합니까? 변수를 먼저 선언하는 것 외에도'if' 블록 안에'global found_state'를 지정하고'1'에 할당해야 할 수도 있습니다. –
자, 아마도'r '# (\ w +)''와'.group (1)'만 있으면 될까요? https://ideone.com/HdPCEt를 참조하십시오 –
약간의 서식을 개선했습니다 – AlBlue