从python中的制表符分隔字符串中获取令牌

时间:2014-11-05 11:37:15

标签: python string

我是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索引处包含整个字符串。我不知道该如何处理?

3 个答案:

答案 0 :(得分: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'

答案 1 :(得分: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")
    ...

答案 2 :(得分: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'