Python의 정규식을 사용하여 파일에서 경로를 추출한 다음 경로를 단일 목록에 추가하려고합니다. 경로는 options
다음과 같이 파일 ( ) 의 다른 줄에 지정 됩니다.
EXE_INC = \
-I$(LIB_SRC)/me/bMesh/lnInclude \
-I$(LIB_SRC)/mTools/lnInclude \
-I$(LIB_SRC)/dynamicM/lnInclude
내가 가진 기능은 다음과 같습니다.
def libinclude():
with open('options', 'r') as options:
for lines in options:
if 'LIB_SRC' in lines:
result = []
lib_src_path = re.search(r'\s*-I\$\(LIB_SRC\)(?P<lpath>\/.*)', lines.strip())
lib_path = lib_src_path.group(1).split()
result.append(lib_path[0])
print result
return (result)
내가 얻는 출력은 다음과 같습니다.
['/me/bMesh/lnInclude']
['/mTools/lnInclude']
['/dynamicM/lnInclude']
그러나 보시다시피 파일의 줄 중 하나에 대해 각각 얻은 세 가지 목록을 얻습니다 options
. 값을 함수 외부에서 사용할 수 있도록 하나의 목록에이 세 경로를 가져 오는 방법이 있습니까?
확실히 일치하는 항목을 찾을 때마다 새 목록을 만들고 있습니다. 따라서 목록을 하나만 만들고 일치하는 항목을 찾을 때마다 계속 추가하세요.
def libinclude():
with open('options', 'r') as options:
result = []
for lines in options:
if 'LIB_SRC' in lines:
lib_src_path = re.search(r'\s*-I\$\(LIB_SRC\)(?P<lpath>\/.*)', lines.strip())
lib_path = lib_src_path.group(1).split()
result.append(lib_path[0])
print result
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다