我在发布之前经历了各种答案,它们都是基于正则表达式的,涉及符号和特殊字符。
这是我的输入文本和预期输出。我想提取“投资目标”和“投资政策”之间的文本
输入文本
“投资目标通过主要投资于非洲公司的投资组合来提供长期资本增长。投资政策”
输出文本:
“通过主要投资于非洲公司的投资组合来提供长期资本增长。”
约书亚答案的替代方案:
input_text="Investment Objective To provide long - term capital growth by investing primarily in a portfolio of African companies. Investment Policy"
start_str = "Investment Objective"
startpos = input_text.find(start_str)
end_str = "Investment Policy"
endpos = input_text.find(end_str)
output_str = input_text[startpos + len(start_str):endpos]
output_str_nospaces = output_str.strip()
print(f"'{output_str}'")
print(f"'{output_str_nospaces}'")
哪个打印:
' To provide long - term capital growth by investing primarily in a portfolio of African companies. '
'To provide long - term capital growth by investing primarily in a portfolio of African companies.'
可以说,您列入黑名单的词是:
black = ["Investment Objective","Investment Policy"]
现在让我们删除它:
for i in black:
input_text = input_text.replace(i,'').strip()
这给出了:
'To provide long - term capital growth by investing primarily in a portfolio of African companies.'