2017-08-02 55 views
0

所以,我有專門的章節和文本這些部分:Python。如何按部分解析文本?

​​

,以及如何從這些部分之一獲取文本?

+0

[與多個分隔符分割字符串?]的可能的複製(https://stackoverflow.com/questions/1059559/split-strings-with-multiple-delimiters) –

+1

閱讀有關[configparser(HTTPS:/ /docs.python.org/3.6/library/configparser.html) – stovfl

+0

謝謝@stovfl。 – Rahul

回答

3
import re 
sections = re.split(r'\[Section\d+\]', text) 

然後,您可以使用列表切片獲得一個節文本。你的情況:

section[1] will give section 1. 
+0

其中之一。並非全部。 – jcommander

+0

@jcommander:請參閱編輯 – Rahul

+0

另請考慮使用[configparser](https://docs.python.org/3.6/library/configparser.html) – Rahul

0

試試這個,

text="""[Section1] 
Some weired text in section 1 

[Section2] 
Some text in section 2 
Some text 
text""" 
print text.split('\n\n') 
>>>['[Section1]\nSome weired text in section 1', '[Section2]\nSome text in section 2\nSome text\ntext'] 
0

如圖所示,該代碼生成每個部分中的線的字典,以便由部分名稱索引。

它通過逐行讀取文件。當它識別出一個節頭時,它會記下這個名字。由於它讀取後續行,直到它讀取下一個標題,它將它們保存在sections中,作爲該名稱下的列表。

如果您不想或不需要線端,請在append聲明中將其去掉。

>>> import re 
>>> patt = re.compile(r'^\s*\[\s*(section\d+)\s*\]\s*$', re.I) 
>>> sections = {} 
>>> with open('to_chew.txt') as to_chew: 
...  while True: 
...   line = to_chew.readline() 
...   if line: 
...    m = patt.match(line) 
...    if m: 
...     section_name = m.groups()[0] 
...     sections[section_name] = [] 
...    else: 
...     sections[section_name].append(line) 
...   else: 
...    break 
...    
>>> sections 
{'Section2': ['Some text in section 2\n', 'Some text\n', 'text'], 'Section1': ['Some weired text in section 1\n', '\n']} 

編輯:簡化代碼。

>>> import re 
>>> patt = re.compile(r'^\s*\[\s*(section\d+)\s*\]\s*$', re.I) 
>>> sections = defaultdict(list) 
>>> with open('to_chew.txt') as to_chew: 
...  for line in to_chew: 
...   m = patt.match(line) 
...   if m: 
...    section_name = m.groups()[0] 
...   else: 
...    sections[section_name].append(line) 
... 
>>> sections 
defaultdict(<class 'list'>, {'Section1': ['Some weired text in section 1\n', '\n'], 'Section2': ['Some text in section 2\n', 'Some text\n', 'text']}) 
+0

UnboundLocalError:在分配之前引用的局部變量'section_name' – jcommander

+0

我懷疑你錯誤地轉錄了因爲我只是再次運行它,並取得成功。 –

+0

代碼可以簡化,如編輯所示。 –