Myluzh Blog

python间隔两个字符串插入一个字符

发布时间: 2022-5-13 文章作者: myluzh 分类名称: Python 朗读文章


import re

subject = '080045000106309140003F2F7D100A01'

if (len(subject) % 2) == 0:
    result = re.sub(r"(?<=\w)(?=(?:\w\w)+$)", "-", subject)
elif (len(subject) % 2) != 0:
    a_subject = subject[:-1]
    b_subject = subject[-1]
    result = re.sub(r"(?<=\w)(?=(?:\w\w)+$)", "-", a_subject) + "-" + b_subject

print(result)
输出内容 08-00-45-00-01-06-30-91-40-00-3F-2F-7D-10-0A-01 

标签: python

发表评论