我需要從下面的文本文件中提取值:複製部分
fdsjhgjhg
fdshkjhk
Start
Good Morning
Hello World
End
dashjkhjk
dsfjkhk
我需要提取的值是從開始到結束。
with open('path/to/input') as infile, open('path/to/output', 'w') as outfile:
copy = False
for line in infile:
if line.strip() == "Start":
copy = True
elif line.strip() == "End":
copy = False
elif copy:
outfile.write(line)
上面我使用的代碼是從這樣一個問題: Extract Values between two strings in a text file using python
該代碼將不包含字符串「開始」和「結束」只是裏面是什麼他們。你會如何包含周邊字符串?
我會用多正則表達式爲 - 的代碼也將尋找更容易 – MaxU