python 正则去除末尾字符串

正则去除末尾字符串

import re
pv = "3724:32263930;3412:36033617;",
pvName = "尺码:XXL;颜色:黑色;",
pv = re.sub(";$", "", pv)
pvName = re.sub(";$", "", pvName)

返回结果:
pv = "3724:32263930;3412:36033617",
pvName = "尺码:XXL;颜色:黑色",

重新拼接字符串

pvName="颜色:松蓝色;尺码:XS"
 if pvName.startswith("颜色"):
     pvName = pvName.split(";")[1]+";"+pvName.split(";")[0]
     pv = pv.split(";")[1]+";"+pv.split(";")[0]

返回结果:
pvName = "尺码:XS;颜色:松蓝色"

posted @ 2021-04-21 14:05  莫贞俊晗  阅读(936)  评论(0编辑  收藏  举报