2013-05-04 109 views
0

我想查看是否存在像test_100.webp这样的文件,然后查看文件test.yaml。因此,我需要从最后去除“_100.webp”模式。我试图使用下面的代码,它给我的问题。从字符串末尾剥离图案

for i, image in enumerate(images_in_item): 
     if image.endswith("_100.webp"): 
      image_strip = image.rstrip(_100.webp) 
      snapshot_markup = os.path.join(image_strip + 'yaml') 
+0

哪些问题是什么呢? – 2013-05-04 07:09:27

+0

尝试'image_strip = image.replace('_ 100.webp','')' – 2013-05-04 07:14:12

+0

基本上它也从字符串中删除其他字符的其他出现。如果使用image.rstrip(“_ 10​​0.webp”),则200_100.webp将减少为2。 – vkaul11 2013-05-04 07:18:10

回答

1

这样做:

suffix = '_100.webp' 
if image.endswith(suffix): 
    image_strip = image[:-len(suffix)] 
    snapshot_markup = os.path.join(image_strip + 'yaml')