我有一個從pdf轉換而來的輸入.txt文件。
我也有50個大綱詞(關鍵字),這些詞通常在輸入文件中已知。
對於50個大綱字中的每一個,我創建了一個輸出文件,我的目的是根據輸入中找到的大綱字,將輸入的.txt文件的內容寫入相關輸出文件中以分割它。在python中寫入文件時錯誤的分割內容
大綱單詞可以在整個文本中找到,但我們專門定位'標題',它們由它們包含大綱單詞前後緊跟換行符的事實確定。我用正則表達式,例如第一個輪廓字:
t = re.search("\nAbduction\n",content, re.I)
但我有49多個這樣的爲每個可能的稱號。因爲我知道t
可能不會返回一個值,所以我應該怎麼做才能爲所有可能的標題返回一個值?
第二個問題:當一個特定的標題被識別出來後,我需要輸出接下來的文本到相應的輸出文件,直到找到另一個標題(或EOF)。我怎樣才能做到這一點 ?
任何幫助,歡迎。
[編輯]這個問題的文本被重新修改,重組。對這種沉重的編輯道歉;它通常更接近原始文本,但在這種情況下,它似乎有助於有一個更重的手...檢查以前的版本[或恢復編輯],如果你認爲否則!
[重新編輯](從張貼的答覆文件OP)
.txt文件的樣本是這樣的:
Abduction
Definition
Abduction is a form of reasoning , sometimes described
as 「deduction in reverse,」 Abduction whereby given a rule that
「A follows from B」 and the observed result of 「A」 we
infer the condition 「B」 of the rule. More generally,
given a theory, T , modeling a domain of interest and
an observation, 「A,」 we infer a hypothesis 「B」 such that
Accuracy
Definition
Accuracy refers to a measure of the degree to which the
predictions of a (cid:55)model match the reality being mod.
它將會像與包括文件的末尾50個頭銜。我已經爲每個標題名稱創建了文件。我寫了一個函數,如:
def TextBetween(self, s, leader, trailer):
end_of_leader = s.index(leader) + len(leader)
if trailer == " ":
return s[end_of_leader:]
else :
start_of_trailer = s.index(trailer, end_of_leader)
return s[end_of_leader:start_of_trailer]
這種計算片頭和片尾之間的內容,所以這個問題是,當我決定片頭和片尾,我想用空格鍵找到正確的title.Because我使用我提到我的正則表達式question.I爲每個標題創建50個正則表達式,並希望在寫入文件時使用它們,但我不知道我是如何實現這一點的。
請更清楚你想要什麼。 – 2012-02-01 00:57:59