如何在python中以列表的形式从子流程命令获取输出?

如何在python中以列表的形式从子流程命令获取输出?,python,perforce,Python,Perforce,我试图使用python脚本检索有关perforce客户端的一些信息。我只想获取与服务器地址、cleint root等相关的信息,如下所示: ping_string = subprocess.Popen(['p4', 'info','ls'], stdout=subprocess.PIPE ).communicate()[0] print ping_string 所以我得到的输出是: User name: hello Client name: My_machine Client host: XY

我试图使用python脚本检索有关perforce客户端的一些信息。我只想获取与服务器地址、cleint root等相关的信息,如下所示:

ping_string = subprocess.Popen(['p4', 'info','ls'], stdout=subprocess.PIPE ).communicate()[0]
print ping_string
所以我得到的输出是:

User name: hello
Client name: My_machine
Client host: XYZ
Current directory: c:\
Peer address: 1.2...
Client address: 1.102....
Server address: abcd
Server root: D:\scc\data
但由于我想检索服务器地址、客户机地址等,所以我希望以列表的形式输出。因此,请建议如何以列表类型获取输出

使用简化了获取命令输出,因此您可以执行以下操作:

out = subprocess.check_output(cmd)

lines = out.splitlines()
请注意,每行将包含尾随的新行字符

或者,如果要在冒号后面添加数据:

lines = [l.split(':', 1)[1].strip() for l in out.splitlines() 
         if ':' in l]
l.split(“:”,1)[1]
取冒号后面的whatever。
.strip()
删除周围的空白。
中的if':'是对不包含冒号的行的保护