Python阻塞字符串

时间:2018-04-25 10:52:47

标签: python

我想从字符串块中删除连字符: e.g:

"(CB)-year-(3F)-year-
(56)-ADDR(01)-DATA(06)-(00)-ADDR5-PBX-CHX-[address0]-(CA)-new-
(56)-ADDR(01)-DATA(03)-(00)-ADDR5-PBX-CHX-[address0]-(CA)-new-
(05)-ADDR5-[address0]-(E0)-tWHR2-nintK-
(56)-ADDR(01)-DATA(05)-(00)-ADDR5-PBX-CHX-[address0]-(CA)-new-"

这就是我的字符串的样子。需要一种通用方法从每行的末尾删除-

  

注意:这是从excel

复制的一个完整字符串

我尝试了以下内容:

 if data.endswith('-'):
        data=seq[:-1]
data.strip("\r\n")

这就是我想要输出/结果的样子:

(CB)-year-(3F)-year
(56)-ADDR(01)-DATA(06)-(00)-ADDR5-PBX-CHX-[address0]-(CA)-new
(56)-ADDR(01)-DATA(03)-(00)-ADDR5-PBX-CHX-[address0]-(CA)-new
(05)-ADDR5-[address0]-(E0)-tWHR2-nintK
(56)-ADDR(01)-DATA(05)-(00)-ADDR5-PBX-CHX-[address0]-(CA)-new

3 个答案:

答案 0 :(得分:1)

string = """"(CB)-year-(3F)-year-
(56)-ADDR(01)-DATA(06)-(00)-ADDR5-PBX-CHX-[address0]-(CA)-new-
(56)-ADDR(01)-DATA(03)-(00)-ADDR5-PBX-CHX-[address0]-(CA)-new-
(05)-ADDR5-[address0]-(E0)-tWHR2-nintK-
(56)-ADDR(01)-DATA(05)-(00)-ADDR5-PBX-CHX-[address0]-(CA)-new-"""

上面的字符串以-结尾,而在python中,完整的字符串被视为单个字符串,因此.endswith()

中的所有其他连字符都不被视为字符串的结尾

字符串刚刚被新行\n分隔,因此您需要先拆分并按以下方式加入它们:

In [12]: print('\n'.join([i[:-1] if i[-1] == '-' else i for i in string.split('\n')]))
"(CB)-year-(3F)-year
(56)-ADDR(01)-DATA(06)-(00)-ADDR5-PBX-CHX-[address0]-(CA)-new
(56)-ADDR(01)-DATA(03)-(00)-ADDR5-PBX-CHX-[address0]-(CA)-new
(05)-ADDR5-[address0]-(E0)-tWHR2-nintK
(56)-ADDR(01)-DATA(05)-(00)-ADDR5-PBX-CHX-[address0]-(CA)-new

<强>逻辑:

'\n'.join(...)将所有字​​符串迭代加入\n

i[:-1]给出没有最后一个字符的字符串

i[-1] == '-'检查字符串的最后一个字符是否以连字符-结尾

string.split('\n')使用分隔符\n拆分字符串会产生一个字符串列表,该列表在列表解析中被迭代

时间比较:

In [18]: %timeit re.sub('-:?$', '', string, flags=re.MULTILINE)
2.74 µs ± 91.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In [19]: %timeit '\n'.join([i[:-1] if i[-1] == '-' else i for i in string.split('\n')])
1.56 µs ± 24.1 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

答案 1 :(得分:1)

使用re,您不必分割字符串并重新构建它。

>>> import re
>>> s = '''abc
... de-f
... ghi-
... jkl--
... mno-:'''
>>> print(re.sub('-:?$', '', s, flags=re.MULTILINE))
abc
de-f
ghi
jkl-
mno
>>> print(re.sub('-+:?$', '', s, flags=re.MULTILINE))
abc
de-f
ghi
jkl
mno

答案 2 :(得分:0)

使用str.rstrip

<强>实施例

s = """"(CB)-year-(3F)-year-
(56)-ADDR(01)-DATA(06)-(00)-ADDR5-PBX-CHX-[address0]-(CA)-new-
(56)-ADDR(01)-DATA(03)-(00)-ADDR5-PBX-CHX-[address0]-(CA)-new-
(05)-ADDR5-[address0]-(E0)-tWHR2-nintK-
(56)-ADDR(01)-DATA(05)-(00)-ADDR5-PBX-CHX-[address0]-(CA)-new-:"""

r = ""
for i in s.split("\n"):
    r += "\n" + i.strip().rstrip("-")
print(r)

<强>输出:

"(CB)-year-(3F)-year
(56)-ADDR(01)-DATA(06)-(00)-ADDR5-PBX-CHX-[address0]-(CA)-new
(56)-ADDR(01)-DATA(03)-(00)-ADDR5-PBX-CHX-[address0]-(CA)-new
(05)-ADDR5-[address0]-(E0)-tWHR2-nintK
(56)-ADDR(01)-DATA(05)-(00)-ADDR5-PBX-CHX-[address0]-(CA)-new-: