简体   繁体   English

获取前两个\\和\\之间的字符串

[英]Get the string between first two \ and \

i have some strings like 我有一些像

mystrinng=\Mango\stone\forest\
mystrinng=\Orange\sand\house\
mystrinng=\Rabbit\cage\village\

I want to get only the first string 我只想得到第一个字符串

Mango
Orange
Rabbit

I got the strings using the below code.Is there any better way to do this other than the below code 我使用下面的代码得到了字符串。除了下面的代码之外,还有什么更好的方法可以做到这一点

    if(mystrinng.startswith('\\')):
                objname=mystrinng[1:].split('\\')[0]

Try this 尝试这个

In [8]: a = ['\\Mango\\stone\\forest\\', '\\Orange\\sand\\house\\', '\\Rabbit\\cage\\village\\']
In [11]: [x.split('\\')[1] for x in a]
Out[11]: ['Mango', 'Orange', 'Rabbit']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM