2014-11-05 41 views
-1

我是python的新手,我需要知道如何分割字符串,以便可以從分割的字符串追加所需的標記。從Python中獲取標記分隔字符串

例如,假設

a = "ALU02021543 Build ISR52 [10G DPoE]Upstream traffic priority is not accurate and scheduler is mussy in one or different ONUs" 

我需要拆分此字符串,並追加「ALU02021543 and [10G DPoE]......」字符串製表符分隔。我嘗試了.split()函數,但它返回的索引號爲0th的字符串包含整個字符串。我不知道如何處理這個問題?

+0

你的意思是你的字符串是' 「ALU02021543 \ tBuild ISR52 \ T [10G的DPoE] \ t Upstram ......」' – gboffi 2014-11-05 11:42:07

+0

可以添加一個完整的願望輸出? – Kasramvd 2014-11-05 11:43:11

+0

@Kasra「ALU02021543 [10G DPoE]上行流量優先級不準確,調度程序在一個或不同的ONU中很麻煩」 - 期望的輸出。 – WarriorPrince 2014-11-05 11:54:07

回答

1

首先條款被寫入,您可以split在你的字符串,然後添加與第一入口第3個到最後:

>>> s=a.split() 

>>> s[0]+' '+' '.join(s[3:]) 
'ALU02021543 [10G DPoE]Upstream traffic priority is not accurate and scheduler is mussy in one or different ONUs' 
+0

謝謝@Kasra,但我需要ALU02021543和[10GDPoE]之間的空間,並且在每個元素之後。 – WarriorPrince 2014-11-05 12:06:10

+0

歡迎,我編輯答案! – Kasramvd 2014-11-05 15:16:50

+0

完美的作品..感謝KT。:) – WarriorPrince 2014-11-06 06:43:24

0

的回答更接近OP問題是

a = "ALU02021543\tBuild\tISR52\t[10G DPoE]\tUpstram traffic priority is not accurate and scheduler is mussy in one or different ONUs" 
t = a.split("\t") 
a_new = " ".join(t[:1]+t[3:]) 
print a_new 
# ALU02021543 [10G DPoE] Upstram traffic priority is not accurate and scheduler is mussy in one or different ONUs 

而一些稍微一般可以在

strings = do(all,the,required,stuff) 
for s in strings: 
    # do stuff with t_list[0], t_list_[2], etc 
    t_list = s.split("\t") 
    ... 
0

嘗試刪除不婉轉的部分牛逼通過re.sub

>>> a = "ALU02021543 Build ISR52 [10G DPoE]Upstream traffic priority is not accurate and scheduler is mussy in one or different ONUs" 
>>> import re 
>>> re.sub(r'\t\w+\t\w+\t', r' ', a) 
'ALU02021543 [10G DPoE]Upstream traffic priority is not accurate and scheduler is mussy in one or different ONUs' 
相關問題