I aim to convert the TOC of The Python Language Reference — Python 3.6.3 documentation to a structured data with the following steps:
Copy contents to a
plr.md
file:In [1]: with open('plr.md') as file: ...: content = file.read() In [2]: content Out[2]: '\n\n- \\1. Introduction\n - [1.1. Alternate Implementations] (https://docs.python.org/3.6/reference/introduction.html#alternate-implementations)\n - [1.2. Notation](https://docs.python.org/3.6/reference/introduction.html#notation)\n- \\2. Lexical analysis\n - [2.1. Line structure] (https://docs.python.org/3.6/reference/lexical_analysis.html#line-structure)\n - [2.2. Other tokens](https://docs.python.org/3.6/reference/lexical_analysis.html#other-tokens)\n
Get chapters:
In [47]: chapters = content.split('\n- \\') ...: #subtract the unqualified part ...: chapters = chapters[1:] In [50]: chapters[0] Out[50]: '1. Introduction\n - [1.1. Alternate Implementations](https://docs.python.org/3.6/reference/introduction.html#alternate-implementations) \n - [1.2. Notation](https://docs.python.org/3.6/reference/introduction.html#notation)'
Separate chapter name and section name in each chapters:
chapter_details = chapters[0].split('\n -') sections = chapter_details[1:] chapter = chapter_details[0] In [54]: chapter Out[54]: '1. Introduction' In [55]: sections Out[55]: [' [1.1. Alternate Implementations](https://docs.python.org/3.6/reference/introduction.html#alternate-implementations)', ' [1.2. Notation](https://docs.python.org/3.6/reference/introduction.html#notation)']
Convert section:
def convert_section(s): start = s.index('[') + 1 end = s.index(']') return s[start:end] In [57]: print(convert_section(' [1.1. Alternate Implementations](https://docs.python.org/3.6/reference/i ...: ntroduction.html#alternate-implementations)')) 1.1. Alternate Implementations sections = map(convert_section, sections) sections = list(sections)
Create a dict:
key = chapter {key:sections} {'1. Introduction':['1.1. Alternate Implementations', '1.2. Notation']}
Encapsulate code in a class and get the result:
class TOC: def __init__(self, filename): self.filename = filename def read(self, filename): with open (filename) as file: content = file.read() return content def convert_section(self, s): start = s.index('[') + 1 end = s.index(']') return s[start:end] def get_chapters(self, filename): content = self.read(filename) chapters = content.split('\n- \\') #subtract the unqualified part chapters = chapters[1:] return chapters def create_chapter_dict(self, chapter): chapter_details = chapter.split('\n -') sections = chapter_details[1:] key = chapter_details[0] value = map(self.convert_section, sections) return {key: list(value)} def get_chapters_dict(self): chapters = self.get_chapters(self.filename) chapters_dict = {} for chapter in chapters: chapter_dict = self.create_chapter_dict(chapter) chapters_dict.update(chapter_dict) return chapters_dict
Run and get the result:
In [89]: TOC('plr.md').get_chapters_dict()
Out[89]:
{'1. Introduction': ['1.1. Alternate Implementations', '1.2. Notation'],
'2. Lexical analysis': ['2.1. Line structure',
'2.2. Other tokens',
'2.3. Identifiers and keywords',
'2.4. Literals',
'2.5. Operators',
'2.6. Delimiters'],
'3. Data model': ['3.1. Objects, values and types',
'3.2. The standard type hierarchy',
'3.3. Special method names',
'3.4. Coroutines'],
This solution is a bit too much for a daily common operation. Is there a standard or easy method for such a task?