简体   繁体   中英

How do I modify the values of an iterator in Python?

As a side project, I'm making a basic HTML parser. The HTML comes in as a string, and I make an iterator for it. I need to remove groups of whitespace at certain points, eg if the character is whitespace, I'd like to consume it, remove it, and advance one by one until I hit a non-whitespace character, where the function would stop.

Iterator setup:

from more_itertools import peekable
chars = peekable("</        html    >")

For example:

"</        html    >

...becomes:

"</html    >"

I think the filter function is what you are looking for.

An example:

import string
text = "</        html    >"

for c in filter(lambda x: x not in string.whitespace, text):
    print(c, end="")

The output is </html>

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