简体   繁体   中英

What is the simple and practical example for using formatter module?

I found a formatter module in Python 3.4 (Lib/formatter.py). I can import it.

import formatter

Then what? What is the purpose of this module? There is no unit test for this module so I can not find any examples. The documentation is very cryptic.

"""Generic output formatting.

Formatter objects transform an abstract flow of formatting events into
specific output events on writer objects..........
"""

Is there any simple and practical example for using this module? I want to know the purpose of this module. Thx.

The module defines an abstract class interface for use with the htmllib.HTMLParser . That module was removed from Python 3; the fact that the formatter module is still there is likely an oversight.

Use it to turn HTML tree events into formatted documents. For example, if during parsing a HTML document a <H1> header is encountered, the formatter.end_paragraph() method will be called, followed by a formatter.push_font() method call.

Use this module if you are transforming HTML documents; like building a HTML-to-plaintext converter, or a web browser.

If you are still confused or unsure about the formatter module, then there is a very good resource which explains it with simple terminology and good examples over here:

http://effbot.org/librarybook/formatter.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM