Use Python format string in reverse for parsing

前端 未结 4 1870
星月不相逢
星月不相逢 2020-12-15 02:33

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条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-15 03:18

    How about:

    id = int(pn.split('-')[1])
    

    This splits the part number at the dash, takes the second component and converts it to integer.

    P.S. I've kept id as the variable name so that the connection to your question is clear. It is a good idea to rename that variable that it doesn't shadow the built-in function.

提交回复
热议问题