2017-08-01 71 views
0

我試圖拆分python 3.6。拆分文本與拆分功能

我需要的是隻有ABC-1.4.0.0

mytext = "_bla.blub = 'abc-1.4.0.0';" 
#print(mytext) 
mytext = str.split("_bla.blub = '"); 
#print (mytext) 
print (mytext[1].split("'")[0]) 

,但我的結果是空的。爲什麼?

+0

我覺得薩瑪是試圖分裂'mytext'的字符串'「_bla.blub =「」'所以它返回1個元素列表,其中有'abc-1.4.0.0';',但有幾個錯誤,我試圖破譯... –

+1

'mytext [13:24]'除非你需要拆分它很花哨,因爲事情正在改變。 – TemporalWolf

+1

如果你取消這些print()的註釋,我想你會看到你的問題。 – TemporalWolf

回答

1

這樣做:

mytext = "_bla.blub = 'abc-1.4.0.0';" 
mytext = str.split(mytext); 
mytext 

['_bla.blub', '=', "'abc-1.4.0.0';"] 

mytext[2] 
"'abc-1.4.0.0';" 

OR

mytext = "_bla.blub = 'abc-1.4.0.0';" 

mytext = mytext.split("_bla.blub = '") 

print (mytext[1].split("'")[0]) 
abc-1.4.0.0 

OR

mytext = "_bla.blub = 'abc-1.4.0.0';" 
mytext = mytext.split("'"); 
mytext 

['_bla.blub', '=', "'abc-1.4.0.0';"] 

mytext[1] 
'abc-1.4.0.0' 
1

你沒有實際作用於mytext

嘗試以下操作:

mytext = "_bla.blub = 'abc-1.4.0.0';" 
#print(mytext) 
mytext = mytext.split("_bla.blub = '") 
#print (mytext) 
print (mytext[1].split("'")[0]) 
1
mytext = "_bla.blub = 'abc-1.4.0.0';" 
print(mytext) 
mytext = mytext.split("'"); 
print (mytext) 
print (mytext[0]) 
print (mytext[1]) 

你需要調用.split()你的字符串,並將其保存到一個變量,而不是在str類叫.split()Try this.

0

試試這個簡單的方法(用單引號分割):

mytext = "_bla.blub = 'abc-1.4.0.0';" 
print(mytext.split("'")[1]) 
0

理想情況下,你應該使用這樣的字符串相關的東西regex模塊。下面是示例代碼從給定的字符串提取所有的單引號之間的字符串:

>>> import re 

>>> mytext = "_bla.blub = 'abc-1.4.0.0';" 
>>> re.findall("'([^']*)'", mytext) 
['abc-1.4.0.0']