2013-04-25 95 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