![](/img/trans.png)
[英]How to remove non-specific char of a string/dataframe[i] in Python
[英]I want to be able to isolate a non-specific string from a list using python
我有这个字符串:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
我希望能够从此字符串中删除“ eth0”部分,但它不一定总是eth0! 但是,其在“ 2:”部分之后的定位是一致的。
有任何想法吗?
编辑:
总体思路是隔离接口名称“ lo”,“ eth0”等。这是一个更长的示例:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 brd 127.255.255.255 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 00:e0:c8:10:00:00 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.1/24 brd 192.168.100.255 scope global eth0
inet 192.168.200.16/32 scope global eth0
inet 192.168.200.17/32 scope global eth0
inet 192.168.200.18/32 scope global eth0
3: gre0: <NOARP> mtu 1476 qdisc noop state DOWN
link/gre 0.0.0.0 brd 0.0.0.0
4: 3g-wan1: <POINTOPOINT,MULTICAST,NOARP> mtu 1500 qdisc noop state DOWN qlen 3
link/ppp
编辑2:新的答案为更新的问题。
使用re.findall(r'\\d+: (.*?):', string)
import re
string = """
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 brd 127.255.255.255 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 00:e0:c8:10:00:00 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.1/24 brd 192.168.100.255 scope global eth0
inet 192.168.200.16/32 scope global eth0
inet 192.168.200.17/32 scope global eth0
inet 192.168.200.18/32 scope global eth0
3: gre0: <NOARP> mtu 1476 qdisc noop state DOWN
link/gre 0.0.0.0 brd 0.0.0.0
4: 3g-wan1: <POINTOPOINT,MULTICAST,NOARP> mtu 1500 qdisc noop state DOWN qlen 3
link/ppp
"""
print re.findall(r'\d+: (.*?):', string)
输出:
['lo', 'eth0', 'gre0', '3g-wan1']
尚不清楚2是否为固定部件。 您可以尝试以下方法:
import re
re.sub("(\d*:)[^:]*:(.*)", "\\1\\2", "2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000")
结果:
'2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000'
编辑:看来我们所有人都错误地理解了您的期望。 @Dogbert的findall
用法将是我的偏爱。 但是,我会提到re.sub
建议书的改编版,以供结案。 您也可以通过调整上面显示的re.sub
函数调用来做到这一点:
>>> re.sub("\d*: *([^:]*):.*", "\\1", "2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000")
'eth0'
这样的简单操作就不需要正则表达式。
使用split()
。 假设s
是您的字符串:
pre, x, after = s.split(': ', 2)
s_without_x = ': '.join([pre, after])
结果:
>>> x
'eth0'
>>> s_without_x
'2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000'
如果要将其应用于任何行,则可以检查if s[0].isdigit()
。
在该命令的输出中,有两件事:
NB: NAME: DESCRIPTION
所以这是一个正则表达式:
>>> s = "2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000"
>>> re.sub('\d+: eth0: ', '', s)
'<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000'
现在可以对其进行参数化:
>>> iface = 'eth0'
>>> s = "2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000"
>>> re.sub(r'\d+: {}: '.format(iface), '', s)
'<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000'
因此您可以使其具有以下功能:
def get_iface_params(iface):
out = subprocess.check_output(['ip', 'addr'])
for s in out:
if 'eth0' in s:
return re.sub(r'\d+: {}: '.format(iface), '', s)
现在,如果使用iface
作为lo
来调用它,它将为您提供lo
接口参数。
高温超导
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.