2013-04-25 61 views
0

我试图解析一组引号之间的随机字符串。 数据的形式总是:在python中解析字符串而不知道字符串

klheafoiehaiofa “randomtextnamehere.ext” fiojeaiof; jidoa; jfioda

我知道.EXT是什么,那就是我要的是randomtextnamehere.ext,和它总是用“”分开。

目前我只能处理某些情况,但我希望能够处理任何情况,如果我可以在第一个时间开始抓住“,并停止在第二个”,那就太好了。 !因为有

致谢存在多于一组“每行的可能性

+0

而且...什么你有没有写你的代码来解决这个问题? – Dusk 2013-04-25 04:15:03

回答

3

可以使用str.split方法:

Docstring: 
S.split([sep [,maxsplit]]) -> list of strings 

Return a list of the words in the string S, using sep as the 
delimiter string. If maxsplit is given, at most maxsplit 
splits are done. If sep is not specified or is None, any 
whitespace string is a separator and empty strings are removed 
from the result. 

In [1]: s = 'klheafoiehaiofa"randomtextnamehere.ext"fiojeaiof;jidoa;jfioda' 

In [2]: s.split('"', 2)[1] 
Out[2]: 'randomtextnamehere.ext' 
+0

好的。第二个参数分裂并不是必要的,在功能上。 – wim 2013-04-25 04:32:23

+1

@wim - 当然你是对的,我试图sh你可以在分隔符的前两次出现处明确地分割字符串* only *。 – root 2013-04-25 04:38:16