Python如何剪出我想要的字符串?

时间:2017-04-14 03:16:54

标签: python string

来自这样的文件的字符串:

aux_source_directory(detail LIB_SRC)
file(GLOB_RECURSE INC_ALL "./*.h")
autogen_ui(LIB_SRC)
add_library(qtdebugger_luadebugger ${LIB_SRC} ${INC_ALL})
target_link_libraries(qtdebugger_luadebugger PUBLIC Qt4::QtGui qtinfra debugger util infra opc)

if(test)
    add_subdirectory(unittest)
endif(test)

我想剪掉target_link_libraries()中的字词。我该怎么办?

1 个答案:

答案 0 :(得分:0)

我试过这种看似有效的方式:

#!/usr/bin/python
# -*- coding: UTF-8 -*- 

import re

file_object = open("C:\Users/vlyu/Perforce/vlyu_GUI/qtgui/debugger/luadebugger/CmakeLists.txt", "r")
try:
	fileText = file_object.read()
	if fileText.find("target_link_libraries") != -1:
		index1 = fileText.index('target_link_libraries(')
		index2 = fileText.index(')', index1)
		print index1 
		print index2
		t1 = fileText[index1:index2+1]
		print t1
finally:
	file_object.close()