2017-08-01 78 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']