split 使用
split作用:把字符串变成列表,这个字符串必须是多行文字。如果是单行文字或一个单词是不行的,实例操作如下: In [46]: output=subprocess.check_output(['df','-h']) In [60]: print(output) b'Filesystem Size Used Avail Use% Mounted on\n/dev/mapper/centos-root 48G 7.6G 40G 16% /\ndevtmpfs 1.9G 0 1.9G 0% /dev\ntmpfs 1.9G 0 1.9G 0% /dev/shm\ntmpfs 1.9G 20M 1.9G 2% /run\ntmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup\n/dev/sda1 1014M 142M 873M 14% /boot\n/dev/mapper/centos-home 23G 40M 23G 1% /home\ntmpfs 1.9G 12K 1.9G 1% /var/lib/kubelet/pods/5cdc5bde-23a2-11e9-8321-000c2935f634/volumes/kubernetes.io~secret/flannel-token-smd42\noverlay 48G 7.6G 40G 16% /var/lib/docker/overlay/5e0d6edc162c3d5e1465e8cb69426423f27b74acbf2cb984fc012acbe0f804f0/merged\nshm 64M 0 64M 0% /var/lib/docker/containers/9c450ecf4d7b750fef3a00f92a7a9a2c536df52041e04546c2350566f1e90206/shm\noverlay 48G 7.6G 40G 16% /var/lib/docker/overlay/1932a1a32f66d39708a632c5e2b767ae5024484350ff7aa6072f46ad65393e1f/merged\ntmpfs 378M 0 378M 0% /run/user/0\n' In [56]: lines=output.decode('utf8').split('\n') In [57]: lines Out[57]: ['Filesystem Size Used Avail Use% Mounted on', '/dev/mapper/centos-root 48G 7.6G 40G 16% /', 'devtmpfs 1.9G 0 1.9G 0% /dev', 'tmpfs 1.9G 0 1.9G 0% /dev/shm', 'tmpfs 1.9G 20M 1.9G 2% /run', 'tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup', '/dev/sda1 1014M 142M 873M 14% /boot', '/dev/mapper/centos-home 23G 40M 23G 1% /home', 'tmpfs 1.9G 12K 1.9G 1% /var/lib/kubelet/pods/5cdc5bde-23a2-11e9-8321-000c2935f634/volumes/kubernetes.io~secret/flannel-token-smd42', 'overlay 48G 7.6G 40G 16% /var/lib/docker/overlay/5e0d6edc162c3d5e1465e8cb69426423f27b74acbf2cb984fc012acbe0f804f0/merged', 'shm 64M 0 64M 0% /var/lib/docker/containers/9c450ecf4d7b750fef3a00f92a7a9a2c536df52041e04546c2350566f1e90206/shm', 'overlay 48G 7.6G 40G 16% /var/lib/docker/overlay/1932a1a32f66d39708a632c5e2b767ae5024484350ff7aa6072f46ad65393e1f/merged', 'tmpfs 378M 0 378M 0% /run/user/0', ''] In [58]: for line in lines[1:-1]: ...: if line: ...: print(line.split()[-2]) ...: 16% 0% 0% 2% 0% 14% 1% 1% 16% 0% 16% 0%