我有这个文件路径:
path ='/home/User/Desktop/Shirt_Nike_Red.jpg'
而且,我想在以下拆分文件路径的结尾:
Cat = Shirt_Nike
Col = Red
这些是我用来分解的代码:
path = '/home/User/Desktop/Shirt_Nike_Red.jpg'
(Cat , Brand , Col) = path.split(os.path.sep)[-1].split("_")
这些是我得到的输出
Cat 'Shirt'
Brand 'Nike'
Col 'Red.jpg'
但是我想要得到以下内容:
Cat = 'Shirt_Nike'
Col = 'Red'
任何有关如何完成该操作的建议将不胜感激,谢谢。
答案 0 :(得分:1)
尝试:
Cat, Col = path.rsplit('.',1)[0].split('/')[-1].rsplit("_", 1)
答案 1 :(得分:1)
您可以使用.rsplit()
Cat, Col = path.split(os.path.sep)[-1].rstrip(".jpg|png").rsplit("_", 1)