如果你只想對(section-level, title)
所有你可以用一個簡單的正則表達式的文件:
import re
codewords = [
'section',
'subsection',
# add other here if you want to
]
regex = re.compile(r'\\({})\{{([^}}]+)\}}'.format('|'.join(re.escape(word) for word in codewords)))
使用範例:
In [15]: text = '''
...: \section{First section}
...:
...: \subsection{Subsection one}
...:
...: Some text
...:
...: \subsection{Subsection two}
...:
...: Other text
...:
...: \subsection{Subsection three}
...:
...: Some other text
...:
...:
...: Also some more text \texttt{other stuff}
...:
...: \section{Second section}
...:
...: \section{Third section}
...:
...: \subsection{Last subsection}
...: '''
In [16]: regex.findall(text)
Out[16]:
[('section', 'First section'),
('subsection', 'Subsection one'),
('subsection', 'Subsection two'),
('subsection', 'Subsection three'),
('section', 'Second section'),
('section', 'Third section'),
('subsection', 'Last subsection')]
通過改變codewords
列表的價值你將能夠匹配更多類型的命令。
若要將此到一個文件只是第一read()
它:
with open('myfile.tex') as f:
regex.findall(f.read())
如果您有保證,所有這些命令都在同一行,那麼你可以更多的內存效率,做到:
與開放( 'myfile.tex')爲f: 結果= [] 在F線:0results.extends(regex.findall(線))
或者,如果你想成爲一個有點莫再花哨:
from itertools import chain
with open('myfile.tex') as f:
results = chain.from_iterable(map(regex.findall, f))
不過請注意,如果你有這樣的:
\section{A very
long title}
這會失敗,爲什麼使用read()
會得到部分過於解決方案。
在你要知道,在格式絲毫的改變將打破這些類型的解決方案,任何情況下。對於更安全的替代方案,您必須尋找適當的LaTeX解析器。
如果你想組一起小節「包含」在一個給定的部分,您可以用上述方案獲得結果後也這樣做。你必須使用類似itertools.groupby
的東西。
從itertools進口GROUPBY,計數,鏈
results = regex.findall(text)
def make_key(counter):
def key(match):
nonlocal counter
val = next(counter)
if match[0] == 'section':
val = next(counter)
counter = chain([val], counter)
return val
return key
organized_result = {}
for key, group in groupby(results, key=make_key(count())):
_, section_name = next(group)
organized_result[section_name] = section = []
for _, subsection_name in group:
section.append(subsection_name)
而最終的結果將是:
In [12]: organized_result
Out[12]:
{'First section': ['Subsection one', 'Subsection two', 'Subsection three'],
'Second section': [],
'Third section': ['Last subsection']}
哪個文本的結構在文章的開頭相匹配。
如果您想使用codewords
列表進行擴展,事情會變得相當複雜。
1)如果你有類似'\ section {方程$ x_ {1 + 2} = 3}'那麼怎麼辦?這裏名字的結尾是**而不是**下一個'}'。或者'\ section {Name \ label {label}}'在某些文檔中經常出現? *任何*正則表達式解決方案都很脆弱,請尋找適當的LaTeX解析器。 2)目前還不清楚你想做什麼。你是否只關心部分/小節的標題等,並希望將它們與它們的級別一起收集起來? – Bakuriu
在我的情況下,確信'{}'僅用於結束某個部分/子部分。我需要處理內容以將Latex文件轉換爲我現有的Neo4j圖形數據庫的Cypher代碼。 – Liam