python数据提取方式

#!/usr/bin/python
#-*- coding:utf-8 -*-
a = """
staff_id,name,age,phone,dept,date
1,Alex Li,22,13651054608,IT,2013-04-01
2,Jack Wang,30,13304320533,HR,2015-05-03
3,Rain Liu,25,13832353222,Sales,2016-04-22
4,Mack Cao,40,1356145343,HR,2009-03-01
"""

aList = [x for x in a.split('\n')[1:-1]]
head = [x for x in aList[0].split(',')]
dList = [dict(zip(head, x.split(','))) for x in aList[1:]]
#print(dList)

import json
print (json.dumps(dList, indent=2))


像下面的top就可以通过这种来分析提取,split(' ')里面接空格
Last login: Wed Apr 25 10:04:04 2018 from 192.168.108.33
top - 18:04:38 up 144 days,  9:19,  2 users,  load average: 0.00, 0.01, 0.05
Tasks: 141 total,   1 running, 140 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.1 us,  0.1 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 16268092 total, 14993260 free,   233616 used,  1041216 buff/cache
KiB Swap: 15998972 total, 15998972 free,        0 used. 15615396 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                 
 7644 root      20   0       0      0      0 S   0.3  0.0   1:07.17 kworker/3:2                             
    1 root      20   0  193700   6840   4060 S   0.0  0.0   4:41.80 systemd                                 
    2 root      20   0       0      0      0 S   0.0  0.0   0:01.33 kthreadd                                
    3 root      20   0       0      0      0 S   0.0  0.0   0:00.42 ksoftirqd/0                             
    5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H                            
    7 root      rt   0       0      0      0 S   0.0  0.0   0:00.05 migration/0                             
    8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_bh                                  
    9 root      20   0       0      0      0 S   0.0  0.0   1:13.49 rcu_sched                               
   10 root      rt   0       0      0      0 S   0.0  0.0   0:20.47 watchdog/0                              
   11 root      rt   0       0      0      0 S   0.0  0.0   0:20.28 watchdog/1                              
   12 root      rt   0       0      0      0 S   0.0  0.0   0:00.32 migration/1                             
   13 root      20   0       0      0      0 S   0.0  0.0   0:00.25 ksoftirqd/1                             
   15 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/1:0H                            
   16 root      rt   0       0      0      0 S   0.0  0.0   0:16.21 watchdog/2                              
   17 root      rt   0       0      0      0 S   0.0  0.0   0:00.17 migration/2                             
   18 root      20   0       0      0      0 S   0.0  0.0   0:00.14 ksoftirqd/2                             
   20 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/2:0H                            
   21 root      rt   0       0      0      0 S   0.0  0.0   0:17.15 watchdog/3                              
   22 root      rt   0       0      0      0 S   0.0  0.0   0:00.30 migration/3                             
   23 root      20   0       0      0      0 S   0.0  0.0   0:02.45 ksoftirqd/3                             
   25 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/3:0H                            
   27 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kdevtmpfs                               
   28 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 netns          

猜你喜欢

转载自www.cnblogs.com/randy-cai/p/8946368.html