回答
import re
sections = re.split(r'\[Section\d+\]', text)
然後,您可以使用列表切片獲得一個節文本。你的情況:
section[1] will give section 1.
其中之一。並非全部。 – jcommander
@jcommander:請參閱編輯 – Rahul
另請考慮使用[configparser](https://docs.python.org/3.6/library/configparser.html) – Rahul
試試這個,
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']
如圖所示,該代碼生成每個部分中的線的字典,以便由部分名稱索引。
它通過逐行讀取文件。當它識別出一個節頭時,它會記下這個名字。由於它讀取後續行,直到它讀取下一個標題,它將它們保存在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']})
UnboundLocalError:在分配之前引用的局部變量'section_name' – jcommander
我懷疑你錯誤地轉錄了因爲我只是再次運行它,並取得成功。 –
代碼可以簡化,如編輯所示。 –
- 1. Python文本解析&分裂
- 2. 在Python中解析MIME正文部分
- 3. 解析文件部分在python
- 4. Python的解析文本文件的僅特定部分
- 5. 如何按行解析文件並拆分文本
- 6. 如何解析部分HTML?
- 7. Python的解析文本和組成不同的部分
- 8. Python解析:lxml只能得到標記文本的一部分
- 9. 解析部分
- 10. 解析部分
- 11. Python的 - 解析文本
- 12. 用Python解析文本
- 13. python解析xml文本
- 14. python中的文本解析
- 15. 在python中解析txt文件很難按分隔符分割
- 16. 用Python解析XML忽略部分
- 17. 的Python塊文本分析
- 18. Python NLTK解析標籤文本:如何檢索標籤文本
- 19. 如何解析文本
- 20. 如何解析文本
- 21. 如何解析文本
- 22. 解析XML部分
- 23. 如何解析部分日期
- 24. 如何用NSURLconnection解析部分xml?
- 25. 如何解析多部分HttpWebResponse
- 26. PERL:按行文本解析腳本行
- 27. 使用Python解析文本文件
- 28. 解析文本文件以CSV在python
- 29. 用標籤解析Python文本文件
- 30. 解析文本文件(Python)的
[與多個分隔符分割字符串?]的可能的複製(https://stackoverflow.com/questions/1059559/split-strings-with-multiple-delimiters) –
閱讀有關[configparser(HTTPS:/ /docs.python.org/3.6/library/configparser.html) – stovfl
謝謝@stovfl。 – Rahul