本文介绍了如何从字符串中删除 WindowsPath 和括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从目录字符串中删除 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: 预期的字符串或类似字节的对象

解决方案
x="(WindowsPath('D:/test/1_birds_bp.png'),WindowsPath('D:/test/1_eagle_mp.png'))"x.replace('WindowsPath(','').replace('(','').replace(')','')

输出:

'D:/test/1_birds_bp.png','D:/test/1_eagle_mp.png'

I need to remove WindowsPath( and some of the closing parentheses ) from a directory string.

x= (WindowsPath('D:/test/1_birds_bp.png'),WindowsPath('D:/test/1_eagle_mp.png'))

What I need to have is

x= ('D:/test/1_birds_bp.png', 'D:/test/1_eagle_mp.png')

I don't know how to do multiple strings at once.

by following @MonkeyZeus I tried;

y = [re.sub("(?<=WindowsPath\(').*?(?='\))",'',a) for  a in x]
解决方案
x= "(WindowsPath('D:/test/1_birds_bp.png'),WindowsPath('D:/test/1_eagle_mp.png'))"

x.replace('WindowsPath(','').replace('(','').replace(')','')

output:

'D:/test/1_birds_bp.png','D:/test/1_eagle_mp.png'

这篇关于如何从字符串中删除 WindowsPath 和括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 12:44
查看更多