【发布时间】:2020-06-05 06:17:25
【问题描述】:
我需要从目录字符串中删除WindowsPath( 和一些右括号)。
x= (WindowsPath('D:/test/1_birds_bp.png'),WindowsPath('D:/test/1_eagle_mp.png'))
我需要的是
x= ('D:/test/1_birds_bp.png', 'D:/test/1_eagle_mp.png')
我不知道如何同时处理多个字符串。
通过关注@MonkeyZeus 我试过了;
y = [re.sub("(?<=WindowsPath\(').*?(?='\))",'',a) for a in x]
TypeError:预期的字符串或类似字节的对象
【问题讨论】:
-
这里显示的不是字符串,而是两个
WindowsPath对象的元组。如果您的问题是“如何将这些对象转换为字符串”,那么答案是:x = [ str(e) for e in x] -
@Błotosmętek 不,我的问题是如何删除它们。
-
@anubhava 好的,请看 OP!
标签: python regex string split strip