如何在Python中将此字符串转换为多维列表?

0 python string dictionary list

我有下一个string:

string = 'tuned     1372                root    6u      REG                8,3      4096  102029349 /tmp/ffiabNswC (deleted)\ngmain     1372 2614           root    6u      REG                8,3      4096  102029349 /tmp/ffiabNswC (deleted)\n'
Run Code Online (Sandbox Code Playgroud)

我需要将每个元素都string放入其中list1[0][..],但是当我看到一个新行'\n'时,我必须将下一个元素放入list1[1][..]

一个多维列表,如下所示:

list1 = [["tuned", "1372", "root", "6u", "REG", "8,3", "4096", "102029349", "/tmp/ffiabNswC", "(deleted)"], 
         ["gmain", "1372", "2614", "root", "6u", "REG", "8,3", "4096", "102029349", "/tmp/ffiabNswC", "(deleted)"]]
Run Code Online (Sandbox Code Playgroud)

我这样做split,但它把我放在同一个维度.

zwe*_*wer 6

首先按新行拆分(获取行),然后按空格拆分每个元素(以获取每列):

data = "tuned 1372 root 6u REG 8,3 4096 102029349 /tmp/ffiabNswC (deleted)\ngmain 1372 2614 root 6u REG 8,3 4096 102029349 /tmp/ffiabNswC (deleted)\n"

parsed = [elements.split() for elements in data.strip().split("\n")]  # `strip()` removes the last whitespace so we don't get blank elements

print(parsed)

# [['tuned', '1372', 'root', '6u', 'REG', '8,3', '4096', '102029349', '/tmp/ffiabNswC', '(deleted)'], ['gmain', '1372', '2614', 'root', '6u', 'REG', '8,3', '4096', '102029349', '/tmp/ffiabNswC', '(deleted)']]
Run Code Online (Sandbox Code Playgroud)