Python 如何解析和拆分文本字符串并将其保存为数组数组

Python 如何解析和拆分文本字符串并将其保存为数组数组,python,regex,string,text,split,Python,Regex,String,Text,Split,我有以下字符串,在本例中有4行,但可能还有更多行: port device name profile settings ==== ====== ============= ======= ================= 1 ttyS1 name1 cas 9600 8N1 ssh none 2 ttyS2 name2 cas 9600 8N1 ssh none

我有以下字符串,在本例中有4行,但可能还有更多行:

  port  device  name           profile  settings
  ====  ======  =============  =======  =================
  1     ttyS1   name1          cas      9600 8N1 ssh none
  2     ttyS2   name2          cas      9600 8N1 ssh none
  3     ttyS3   name3          cas      9600 8N1 ssh none
  4     ttyS4   name4          cas      9600 8N1 ssh none

Press any key to continue...
我只需要从port和name列获取数据,并将其分配给一个数组数组或多个数组

e、 g

有谁能告诉我如何才能做到这一点

import re

txt = """
  port  device  name           profile  settings
  ====  ======  =============  =======  =================
  1     ttyS1   name1          cas      9600 8N1 ssh none
  2     ttyS2   name2          cas      9600 8N1 ssh none
  3     ttyS3   name3          cas      9600 8N1 ssh none
  4     ttyS4   name4          cas      9600 8N1 ssh none

Press any key to continue...
"""

r = re.compile(r"^[^\d]+(\d+)\s*[^\s]+\s*([^\s]+)", flags=re.M)

out = r.findall(txt)
print(out)
印刷品:

['1','name1','2','name2','3','name3','4','name4']
有了您展示的样品,请尝试以下内容。使用Python3.8编写并测试,使用Python的findall函数

import re
var="""  port  device  name           profile  settings
====  ======  =============  =======  =================
1     ttyS1   name1          cas      9600 8N1 ssh none
2     ttyS2   name2          cas      9600 8N1 ssh none
3     ttyS3   name3          cas      9600 8N1 ssh none
4     ttyS4   name4          cas      9600 8N1 ssh none"""

re.findall(r'^\s+?(\d+).*?\S+\s+(\S+).*$',var,re.M)
输出如下:

[('1', 'name1'), ('2', 'name2'), ('3', 'name3'), ('4', 'name4')]
说明:添加上述正则表达式的详细说明

^\s+?      ##Checking from starting of value spaces occurrences 1 or more optional.
(\d+)      ##Creating 1st capturing group which has continuous digits in it, port number.
.*?\S+\s+  ##using non greedy match to match till non space value(1 or more) followed by 1 or more spaces.
(\S+)      ##Creating 2nd capturing group which has all non space values(names) in it.
.*$        ##Matching rest of the values here.

另一个用户已经得到了最好的解决方案,但这是我的简单解决方案:D

stringOrig = """
  port  device  name           profile  settings
  ====  ======  =============  =======  =================
  1     ttyS1   name1          cas      9600 8N1 ssh none
  2     ttyS2   name2          cas      9600 8N1 ssh none
  3     ttyS3   name3          cas      9600 8N1 ssh none
  4     ttyS4   name4          cas      9600 8N1 ssh none

Press any key to continue...
"""

stringLines = stringOrig.split("\n")
linesContent = stringLines[3:-3]
lineValues = list(
  map(lambda l: 
      list(
        filter(lambda x : x != "", l.split(" "))
      )[0:3:2]
      , linesContent
  )
)
print(lineValues)

有具体问题吗?请看。
stringOrig = """
  port  device  name           profile  settings
  ====  ======  =============  =======  =================
  1     ttyS1   name1          cas      9600 8N1 ssh none
  2     ttyS2   name2          cas      9600 8N1 ssh none
  3     ttyS3   name3          cas      9600 8N1 ssh none
  4     ttyS4   name4          cas      9600 8N1 ssh none

Press any key to continue...
"""

stringLines = stringOrig.split("\n")
linesContent = stringLines[3:-3]
lineValues = list(
  map(lambda l: 
      list(
        filter(lambda x : x != "", l.split(" "))
      )[0:3:2]
      , linesContent
  )
)
print(lineValues)