Use Python format string in reverse for parsing

前端 未结 4 1520
醉酒成梦
醉酒成梦 2020-12-15 03:03

I\'ve been using the following python code to format an integer part ID as a formatted part number string:

pn = \'PN-{:0>9}\'.format(id)

4条回答
  •  Happy的楠姐
    2020-12-15 03:34

    The parse module "is the opposite of format()".

    Example usage:

    >>> import parse
    >>> format_string = 'PN-{:0>9}'
    >>> id = 123
    >>> pn = format_string.format(id)
    >>> pn
    'PN-000000123'
    >>> parsed = parse.parse(format_string, pn)
    >>> parsed
    
    >>> parsed[0]
    '123'
    

提交回复
热议问题