2016-06-30 5 views
0

私は、マルチプロセッシングモジュールを使用して並列に実行されているが正しい順序で出力を表示しないいくつかの方法があります。PythonのIPアドレスで標準出力を並べ替えます

Host:10.76.0.114 Running upon kernel reinstallation for GRUB 
Host:10.76.0.114 Running: /sbin/new-kernel-pkg --package kernel --mkinitrd --dracut --depmod --install --multiboot=/boot/xen.gz 3.18.34-20.el6.x86_64 

Host:10.76.0.121 Running %pre for the backup-tools 
Host:10.76.0.112 Setting up Update Process 
Host:10.76.0.121 Running: yum -y -q update httpd 

私のようなものが必要な場合:

Host:10.76.0.112 Setting up Update Process 
Host:10.76.0.114 Running upon kernel reinstallation for GRUB 
Host:10.76.0.114 Running: /sbin/new-kernel-pkg --package kernel --mkinitrd 
Host:10.76.0.121 Running %pre for the backup-tools 
Host:10.76.0.121 Running: yum -y -q update httpd 

を、私はそれがテストスクリプトを使用して、それに問題が作成しようとしていた。たとえば

import StringIO 
import string, sys 

stdout = sys.stdout 

sys.stdout = file = StringIO.StringIO() 

print """ 
10.76.0.114 Initiate Recovery images download 
10.76.0.114 Running: wget -b -N -i /tools/recovery/recovery-templates-url.list -o /tools/recovery/recovery-templates-download.log -P /tools/recovery >/dev/null 2>&1 
10.76.0.114 2016-06-30 00:33 Finished Xen Hypervisor '4.2' and StorageAPI '4.2' install 
10.76.0.114 
10.76.0.113 Preparing...    ########################################### [100%] 
10.76.0.113 package 3-2.noarch is already installed 


""" 

sys.stdout = stdout 

l=file.getvalue() 
#print l 
l=str(l) 
l=l.split() 
#print l 
print sorted(l, key=lambda k:k[0]) 

私は次の出力を得ます:

['###########################################', "'4.2'", "'4.2'", '-b', '-N', '-i', '-o', '-P', '/tools/recovery/recovery-templates-url.list', '/tools/recovery/recovery-templates-download.log', '/tools/recovery', '00:33', '10.76.0.114', '10.76.0.114', '10.76.0.114', '10.76.0.114', '10.76.0.113', '10.76.0.113', '10.76.0.113', '2>&1', '2016-06-30', '>/dev/null', 'Finished', 'Hypervisor', 'Initiate', 'Preparing...', 'Recovery', 'Running:', 'Retrieving', 'StorageAPI', 'Xen', '[100%]', 'and', 'already', 'download', 'images', 'install', 'is', 'installed', , 'package', 'wget' 

Hこの場合、上記の正しい出力を得るために、IPアドレスでソートすることはできますか?

助けてください!

答えて

2

ここで私はいくつかを変更した:

>>> for sorted_line in sorted(lines, key=lambda line: line.split(' ')[0]): 
...  print sorted_line 
10.76.0.113 Preparing...    ########################################### [100%] 
10.76.0.113 package 3-2.noarch is already installed 
10.76.0.114 Initiate Recovery images download 
10.76.0.114 Running: wget -b -N -i /tools/recovery/recovery-templates-url.list -o /tools/recovery/recovery-templates-download.log -P /tools/recovery >/dev/null 2>&1 
10.76.0.114 2016-06-30 00:33 Finished Xen Hypervisor '4.2' and StorageAPI '4.2' install 
10.76.0.114 
:ここ

import StringIO 
import string, sys 

stdout = sys.stdout 

sys.stdout = file = StringIO.StringIO() 

print """ 
10.76.0.114 Initiate Recovery images download 
10.76.0.114 Running: wget -b -N -i /tools/recovery/recovery-templates-url.list -o /tools/recovery/recovery-templates-download.log -P /tools/recovery >/dev/null 2>&1 
10.76.0.114 2016-06-30 00:33 Finished Xen Hypervisor '4.2' and StorageAPI '4.2' install 
10.76.0.114 
10.76.0.113 Preparing...    ########################################### [100%] 
10.76.0.113 package 3-2.noarch is already installed 


""" 

sys.stdout = stdout 

text = file.getvalue() 
lines = [line for line in text.splitlines() if line] 
print sorted(lines, key=lambda line: line.split(' ')[0]) 

がo/pは次のようになります方法です

関連する問題