当前位置: 首页>>代码示例>>Python>>正文


Python Suppress.parseFile方法代码示例

本文整理汇总了Python中pyparsing.Suppress.parseFile方法的典型用法代码示例。如果您正苦于以下问题:Python Suppress.parseFile方法的具体用法?Python Suppress.parseFile怎么用?Python Suppress.parseFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pyparsing.Suppress的用法示例。


在下文中一共展示了Suppress.parseFile方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: songs_pyparsing

# 需要导入模块: from pyparsing import Suppress [as 别名]
# 或者: from pyparsing.Suppress import parseFile [as 别名]
def songs_pyparsing(fh):
    r"""
    >>> import os
    >>> filename = os.path.dirname(__file__)
    >>> filename = os.path.join(filename, "data/Various-Pop.m3u")
    >>> with open(filename, "rt", encoding="utf8") as fh:
    ...     songs = songs_pyparsing(fh)
    >>> songs[0].title, songs[0].seconds, songs[0].filename
    ('Various - Two Tribes', 236, 'Various\\Frankie Goes To Hollywood\\02-Two Tribes.ogg')
    >>> songs[-1].title, songs[-1].seconds, songs[-1].filename
    ('The Police - Walking On The Moon', 303, 'Various\\Sting & The Police 1997\\06-Walking On The Moon.ogg')
    >>> lines = []
    >>> lines.append("#EXTM3U")
    >>> lines.append("#EXTINF:140,The Beatles - Love Me Do")
    >>> lines.append("Beatles\\Greatest Hits\\01-Love Me Do.ogg")
    >>> lines.append("#EXTINF:-1,The Beatles - From Me To You")
    >>> lines.append("Beatles\\Greatest Hits\\02-From Me To You.ogg")
    >>> import io
    >>> data = io.StringIO("\n".join(lines))
    >>> songs = songs_ply(data)
    >>> len(songs) == 2
    True
    >>> songs[0].title, songs[0].seconds
    ('The Beatles - Love Me Do', 140)
    >>> songs[1].title, songs[1].seconds
    ('The Beatles - From Me To You', -1)
    """

    def add_song(tokens):
        songs.append(Song(tokens.title, tokens.seconds,
                          tokens.filename))
        #songs.append(Song(**tokens.asDict()))

    songs = []
    title = restOfLine("title")
    filename = restOfLine("filename")
    seconds = Combine(Optional("-") + Word(nums)).setParseAction(
            lambda tokens: int(tokens[0]))("seconds")
    info = Suppress("#EXTINF:") + seconds + Suppress(",") + title
    entry = info + LineEnd() + filename + LineEnd()
    entry.setParseAction(add_song)
    parser = Suppress("#EXTM3U") + OneOrMore(entry)
    try:
        parser.parseFile(fh)
    except ParseException as err:
        print("parse error: {0}".format(err))
        return []
    return songs
开发者ID:178220709,项目名称:PyHello,代码行数:50,代码来源:ReadM3U.py


注:本文中的pyparsing.Suppress.parseFile方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。