Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在解析之前,如何为字符串变量“interface_info”中的每一行剥离前缘空间?_Python_Regex - Fatal编程技术网

Python 在解析之前,如何为字符串变量“interface_info”中的每一行剥离前缘空间?

Python 在解析之前,如何为字符串变量“interface_info”中的每一行剥离前缘空间?,python,regex,Python,Regex,输出: import re interface_info = ''' phy#3 Interface wlan1-cabin-2 ifindex 37 wdev 0x300000003 addr 06:53:1a:4e:07:02 ssid SSIDTEST3

输出:

import re     
interface_info = '''  
    phy#3  
            Interface wlan1-cabin-2  
                    ifindex 37  
                    wdev 0x300000003  
                    addr 06:53:1a:4e:07:02  
                    ssid SSIDTEST3  
                    type AP  
                    channel 6 (2437 MHz), width: 20 MHz, center1: 2437 MHz  
            Interface wlan1-cabin-1  
                    ifindex 36  
                    wdev 0x300000002  
                    addr 06:53:1a:4e:07:01  
                    ssid SSIDTEST2  
                    type AP  
                    channel 6 (2437 MHz), width: 20 MHz, center1: 2437 MHz  
            Interface wlan1  
                    ifindex 7  
                    wdev 0x300000001  
                    addr 06:53:1a:4e:07:00  
                    ssid SSID1  
                    type AP  
                    channel 6 (2437 MHz), width: 20 MHz, center1: 2437 MHz  
    phy#2  
            Interface wlan0  
                    ifindex 6  
                    wdev 0x200000001  
                    addr 00:30:1a:4e:07:ac  
                    type managed  
    '''  
    stripped = interface_info.lstrip(' \t\n\r')  
    ssid_regex = re.compile('Interface wlan1-cabin-2+((.*\n){6})')  
    ssid_extract = re.search(ssid_regex, stripped)  
    interface_split = re.split(r'\n', ssid_extract.group(0))  
    ssid = str(interface_split[4]).strip(' ssid ')  

    print(stripped)
    print (ssid_extract)
    print str(interface_split)
    print (ssid)

[“接口wlan1-CAB-2”、“ifindex 37”、“wdev 0x30000003”、“地址06:53:1a:4e:07:02”、“ssid SSIDTEST3”、“类型AP”和“
SSIDTEST3`
在上述代码的输出中,请注意列表中的每个字符串都有 前沿空间。我正试图在比赛开始前绊倒/剥夺这些空间 字符串在列表中结束。

使用将只删除
phy#3

您所能做的是对使用和映射拆分返回的所有项目使用strip:

如果您只想删除左侧的空白字符,另一种方法可以是首先从字符串中删除所有前导的空白字符,方法是使用
^\s+
,并使用多行标志编译正则表达式:

interface_split = map(str.strip, re.split(r'\n', ssid_extract.group(0)))

出于某种原因,请参见

“我的帖子”的“输出:部分”中删除了空格<代码>[“接口wlan1-CAB-2”、“ifindex 37”、“wdev 0x30000003”、“地址06:53:1a:4e:07:02”、“ssid SSIDTEST3”、“类型AP”、“SSIDTEST3”
interface_split = map(str.strip, re.split(r'\n', ssid_extract.group(0)))
stripped = re.sub(re.compile('^\s+', re.MULTILINE), '', interface_info)