str.splitlines([keepends]) Return a list of the lines in the string,
breaking at line boundaries. This method uses the universal newlines
approach to splitting lines. Line breaks are not included in the
resulting list unless keepends is given and true.
For example, 'ab c\n\nde fg\rkl\r\n'.splitlines() returns ['ab c', '',
'de fg', 'kl'], while the same call with splitlines(True) returns ['ab
c\n', '\n', 'de fg\r', 'kl\r\n'].
Unlike split() when a delimiter string sep is given, this method
returns an empty list for the empty string, and a terminal line break
does not result in an extra line.
此方法将运行shell命令并返回行列表:
def run_shell_command_multiline(cmd):
p = subprocess.Popen([cmd], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
if p.returncode != 0:
raise RuntimeError("%r failed, status code %s stdout %r stderr %r" % (
cmd, p.returncode, stdout, stderr))
return stdout.splitlines() # This is the stdout from the shell command
您需要使用
splitlines
方法按行分割输出此方法将运行shell命令并返回行列表:
使用
.readlines()
已经将输出分成几行。你知道吗如果您想摆脱额外的
\n
,还可以使用.strip()
。你知道吗相关问题 更多 >
编程相关推荐