Use Python format string in reverse for parsing

前端 未结 4 1517
醉酒成梦
醉酒成梦 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条回答
  •  粉色の甜心
    2020-12-15 03:09

    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.

提交回复
热议问题