怎么把图片中的数据转换成 Python字典 以键值对形式 例如: PID:11 USERNAME: root .....
7 个解决方案
#1
pandas包可以解决这个问题
#2
一个zip()不就搞定了?
#3
a = """
top - 10:02:23 up 155 days, 13:10, 3 users, load average: 2.02, 2.01, 2.04
Tasks: 663 total, 3 running, 659 sleeping, 0 stopped, 1 zombie
%Cpu(s): 11.1 us, 13.5 sy, 0.0 ni, 75.4 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 13181207+total, 65516756 free, 1342132 used, 64953184 buff/cache
KiB Swap: 4194300 total, 4194300 free, 0 used. 12560134+avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
68418 root 20 0 14760 820 668 R 100.0 0.0 5126086h proxseas
92099 songyz 20 0 130560 2240 1216 R 11.8 0.0 0:00.03 top
250 root 20 0 0 0 0 S 5.9 0.0 677:26.31 rcu_sched
1 root 20 0 54256 4148 2472 S 0.0 0.0 19:43.00 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:01.86 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:58.02 ksoftirqd/0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
8 root rt 0 0 0 0 S 0.0 0.0 0:21.00 migration/0
9 root 20 0 0 0 0 S 0.0 0.0 0:04.94 rcu_bh
10 root 20 0 0 0 0 S 0.0 0.0 0:01.27 rcuob/0
11 root 20 0 0 0 0 S 0.0 0.0 0:00.97 rcuob/1
12 root 20 0 0 0 0 S 0.0 0.0 0:00.96 rcuob/2
13 root 20 0 0 0 0 S 0.0 0.0 0:00.91 rcuob/3
14 root 20 0 0 0 0 S 0.0 0.0 0:00.07 rcuob/4
15 root 20 0 0 0 0 S 0.0 0.0 0:00.02 rcuob/5
16 root 20 0 0 0 0 S 0.0 0.0 0:00.01 rcuob/6
17 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/7
18 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/8
19 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/9
20 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/10
21 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/11
22 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/12
23 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/13
24 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/14
25 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/15
26 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/16
27 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/17
28 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/18
29 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/19
30 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/20
"""
aList = [x for x in a.split('\n')[7:-1]]
head = [x for x in aList[0].split()]
dList = [dict(zip(head, x.split())) for x in aList[1:]]
import json
print json.dumps(dList, indent=4)
#4
都是好答案!
#5
大神,我的COMMAND中字符比较多
#6
>>> "a b c d".split(' ', 2)
['a', 'b', 'c d']
#7
谢谢,你知道用正则表达式怎么处理吗?
#1
pandas包可以解决这个问题
#2
一个zip()不就搞定了?
#3
a = """
top - 10:02:23 up 155 days, 13:10, 3 users, load average: 2.02, 2.01, 2.04
Tasks: 663 total, 3 running, 659 sleeping, 0 stopped, 1 zombie
%Cpu(s): 11.1 us, 13.5 sy, 0.0 ni, 75.4 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 13181207+total, 65516756 free, 1342132 used, 64953184 buff/cache
KiB Swap: 4194300 total, 4194300 free, 0 used. 12560134+avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
68418 root 20 0 14760 820 668 R 100.0 0.0 5126086h proxseas
92099 songyz 20 0 130560 2240 1216 R 11.8 0.0 0:00.03 top
250 root 20 0 0 0 0 S 5.9 0.0 677:26.31 rcu_sched
1 root 20 0 54256 4148 2472 S 0.0 0.0 19:43.00 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:01.86 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:58.02 ksoftirqd/0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
8 root rt 0 0 0 0 S 0.0 0.0 0:21.00 migration/0
9 root 20 0 0 0 0 S 0.0 0.0 0:04.94 rcu_bh
10 root 20 0 0 0 0 S 0.0 0.0 0:01.27 rcuob/0
11 root 20 0 0 0 0 S 0.0 0.0 0:00.97 rcuob/1
12 root 20 0 0 0 0 S 0.0 0.0 0:00.96 rcuob/2
13 root 20 0 0 0 0 S 0.0 0.0 0:00.91 rcuob/3
14 root 20 0 0 0 0 S 0.0 0.0 0:00.07 rcuob/4
15 root 20 0 0 0 0 S 0.0 0.0 0:00.02 rcuob/5
16 root 20 0 0 0 0 S 0.0 0.0 0:00.01 rcuob/6
17 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/7
18 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/8
19 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/9
20 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/10
21 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/11
22 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/12
23 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/13
24 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/14
25 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/15
26 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/16
27 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/17
28 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/18
29 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/19
30 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/20
"""
aList = [x for x in a.split('\n')[7:-1]]
head = [x for x in aList[0].split()]
dList = [dict(zip(head, x.split())) for x in aList[1:]]
import json
print json.dumps(dList, indent=4)
#4
都是好答案!
#5
大神,我的COMMAND中字符比较多
#6
>>> "a b c d".split(' ', 2)
['a', 'b', 'c d']
#7
谢谢,你知道用正则表达式怎么处理吗?