【问题标题】:I need to split a value in the variable我需要在变量中拆分一个值
【发布时间】:2019-05-30 21:27:36
【问题描述】:

我需要按照以下方式拆分下面代码中定义的路径

k[0] = /dev/mapper/centos-root
k[1] = 52403200
k[2] = 14460252 an so on.

我尝试了什么? 我尝试使用 split 功能,但目前需要 k[0] = / 另外,我尝试将它传递到一个列表中,其中整个上下文作为一个值

command = ("df -P | egrep '([20][0-9]|100)%' |  awk '{print}'|sed -e 's/ / : /g'")
command_execute = (stdin, stdout, stderr) = ssh.exec_command(command)
read_contents = stdout.read().decode("utf-8")
read_contents
'/dev/mapper/centos-root :  :  :  : 52403200 : 14460252 :  : 37942948 :  :  :  :  :  : 28% : /\n'
k=read_contents[0]
k
'/'
k=read_contents[1]
k
'd'
k.split(" ")
['d']
m =k.split(":")
m
['d']
s = [read_contents]
s
['/dev/mapper/centos-root :  :  :  : 52403200 : 14460252 :  : 37942948 :  :  :  :  :  : 28% : /\n']
s[0]
'/dev/mapper/centos-root :  :  :  : 52403200 : 14460252 :  : 37942948 :  :  :  :  :  : 28% : /\n'
s[1]
Traceback (most recent call last):
  File "<input>", line 1, in <module>
IndexError: list index out of range
s[0]
'/dev/mapper/centos-root :  :  :  : 52403200 : 14460252 :  : 37942948 :  :  :  :  :  : 28% : /\n'

k[0] = /dev/mapper/centos-root
k[1] = 52403200
k[2] = 14460252 an so on.

【问题讨论】:

    标签: python unix centos


    【解决方案1】:

    当您在: 上拆分时,您希望去掉带有空格的字符串

    In [20]: read_contents = '/dev/mapper/centos-root :  :  :  : 52403200 : 14460252 :  : 37942948 :  :  :  :  :  : 28% : /\n'                                                                                                 
    #Get rid of empty strings, and strip leading and trailing whitespaces around the rest of the words
    In [21]: contents = [content.strip() for content in read_contents.split(':') if content.strip()]                                                                                                                           
    
    In [22]: contents                                                                                                                                                                                                          
    Out[22]: ['/dev/mapper/centos-root', '52403200', '14460252', '37942948', '28%', '/']
    
    In [23]: for item in contents: 
        ...:     print(item) 
        ...:                                                                                                                                                                                                                   
    /dev/mapper/centos-root
    52403200
    14460252
    37942948
    28%
    /
    

    【讨论】:

      【解决方案2】:

      另一种选择是使用filter 函数按预期数字长度进行选择。

      read_contents = '/dev/mapper/centos-root :  :  :  : 52403200 : 14460252 :  : 3794...: 2948 :  :  :  :  :  : 28% : /\n'
      k = list(filter(lambda x: len(x)>5, read_contents.split(':')))
      

      输出:

      print(k)
      ['/dev/mapper/centos-root ', ' 52403200 ', ' 14460252 ', ' 3794...', ' 2948 ']
      

      【讨论】:

      • 很好的解决方案,但无法实施,因为长度可能会不时变化
      【解决方案3】:
      import re
      a='/dev/mapper/centos-root :52403200 : 14460252 :  : 37942948 :  :  :  :  :  : 28% : /\n'
      print(re.split(r'(?:\s+:)+',a))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-26
        • 2022-11-25
        • 1970-01-01
        • 1970-01-01
        • 2012-02-11
        • 2018-05-13
        相关资源
        最近更新 更多