__init__.py 文件源码

python
阅读 18 收藏 0 点赞 0 评论 0

项目:WhatTheHack 作者: Sylphias 项目源码 文件源码
def smartsplit(string, sep):
    """Split while allowing escaping.

    So far, this seems to do what I expect - split at the separator,
    allow escaping via \, and allow the backslash itself to be escaped.

    One problem is that it can raise a ValueError when given a backslash
    without a character to escape. I'd really like a smart splitter
    without manually scan the string. But maybe that is exactly what should
    be done.
    """
    assert string is not None   # or shlex will read from stdin
    if not six.PY3:
        # On 2.6, shlex fails miserably with unicode input
        is_unicode = isinstance(string, unicode)
        if is_unicode:
            string = string.encode('utf8')
    l = shlex.shlex(string, posix=True)
    l.whitespace += ','
    l.whitespace_split = True
    l.quotes = ''
    if not six.PY3 and is_unicode:
        return map(lambda s: s.decode('utf8'), list(l))
    else:
        return list(l)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号