我想修改一個〜43k行的txt文件。在文件中給出命令* Nset後,我需要提取並保存該命令後面的所有行,並在文件中的下一個*命令時停止。在每個命令之後有不同數量的行和字符。舉例來說,這裏的文件的樣本部分:通過文件重複標題之間的提取行
*Nset
1, 2, 3, 4, 5, 6, 7,
12, 13, 14, 15, 16,
17, 52, 75, 86, 92,
90, 91, 92 93, 94, 95....
*NEXT COMMAND
blah blah blah
*Nset
numbers
*Nset
numbers
*Command
irrelevant text
我現在有工作的代碼時,我需要的數字是不是之間有兩個* N set個的。當一個* Nset跟隨另一個的數字時,它會跳過該命令和程序線,我不知道爲什麼。當下一個命令不是* Nset時,它會找到下一個命令並將數據完美地提取出來。
import re
# read in the input deck
deck_name = 'master.txt'
deck = open(deck_name,'r')
#initialize variables
nset_data = []
matched_nset_lines = []
nset_count = 0
for line in deck:
# loop to extract all nset names and node numbers
important_line = re.search(r'\*Nset,.*',line)
if important_line :
line_value = important_line.group() #name for nset
matched_nset_lines.insert(nset_count,line_value) #name for nset
temp = []
# read lines from the found match up until the next *command
for line_x in deck :
if not re.match(r'\*',line_x):
temp.append(line_x)
else :
break
nset_data.append(temp)
nset_count = nset_count + 1
我正在使用Python 3.5。謝謝你的幫助。
是否有一個命令* always *在一行的開頭,以'「*」'開始? –
@ juanpa.arrivillaga,是的。有各種各樣的命令,但是在每個命令之前是「*」。然後下一行是數字。 –
這可能與所有相關嗎? https://stackoverflow.com/questions/25943000/finding-a-word-between-two-words-that-will-not-match-if-the-closing-word-occurs –