【问题标题】:Python one print command two variables output two diffent lines to screenPython 一个打印命令两个变量输出两个不同的行到屏幕
【发布时间】:2014-09-02 12:50:44
【问题描述】:

我有一个可通过 Apache 访问的 php 脚本,它调用 python 脚本来 cat /etc/redhat-release 在两台服务器上并返回结果并将它们分配为两个不同的变量。然后我将这两个变量打印到屏幕上。问题是它将它们打印在屏幕上的同一行:['Red Hat Enterprise Linux Server release 6.5 (Santiago)\n', 'Red Hat Enterprise Linux Server release 6.2 (Santiago)\n']

我尝试在 to 之间插入 \n 或“\n”,但出现错误: SyntaxError: 无效语法

File "/var/www/html/php/check_version.py", line 35
  print first + \n second
                        ^
SyntaxError: unexpected character after line continuation character

如果我将它们打印在两行上,它只会给我最后一个变量。例如:

print first
print second ( I only get this result on the screen )

我错过了什么?

这是我的脚本:

#!/usr/bin/python

import paramiko, os, string, threading
import getpass
import socket
import sys

firsthost = '192.168.1.4'

ssh = paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect(firsthost, username='user', password='password')

stdin, stdout, stderr = ssh.exec_command('cat /etc/redhat-release')

first = stdout.readlines()

secondhost = '192.168.1.5'

ssh = paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect(secondhost, username='user', password='password')

stdin, stdout, stderr = ssh.exec_command('cat /etc/redhat-release')

second = stdout.readlines()

print first + second

ssh.close()

【问题讨论】:

  • 添加了python-2.6标签,移除了变量标签

标签: php python linux newline python-2.6


【解决方案1】:

注意:您目前正在使用以下命令从标准输出中提取信息:

first = stdout.readlines()
second = stdout.readlines()

这将返回一个列表,然后当您将它们添加在一起时会打印出一个列表。你不想要那个。您希望它们成为字符串(在过多的换行符上剥离它们也是一个好主意),因此您需要执行以下操作:

first = stdout.readline().strip()
second = stdout.readline().strip()

从那里你可以用标签将它们回显到 html 中以分隔行:

print "{0}</br>{1}".format(first,second)

但是,如果您在获取有效输出时遇到问题(正如您在许多 cmets 中所说的那样),您可能需要在 php 界面之前手动运行脚本以确定它是否可以正常工作。让它简单地打印变量,如果它不能,那么你的 ssh exec_command 有问题。

【讨论】:

  • 鲍勃,您的建议通过做两件事起作用:1. 我添加了您的打印语句和 2. 我注意到您的变量在 stdout.readlines 中没有像我那样的 s。我删除了 s 并且它起作用了。使用 s 它没有给我任何输出和错误: Traceback(最近一次调用最后一次):文件“/var/www/html/php/check_version.py”,第 18 行,在 first = stdout.readlines() .strip() AttributeError: 'list' 对象没有属性'strip。问题解决了。谢谢!!
  • 是的,因为 readlines() 返回一个列表,你需要一个字符串。很高兴我能帮上忙。
【解决方案2】:

您忘记了 +(连接字符串)和 '\n' 周围的引号(使其成为字符串):

>>> print 'a' + '\n' + 'b'
a
b

我更喜欢format

>>> print 'a{}b'.format('\n')
a
b

编辑:正如@Bob 所说,在您的旧 Python 版本中,您需要发布

'a{0}b'.format('\n')

第一个 sn-p 在 2.6 中对我来说工作正常。

【讨论】:

  • 根据 python 版本,可能需要提及"a{0}b".format("\n") 可能是明智的。或者您可以简单地使用"a%sb" % "\n",它将涵盖更多版本。
  • 感谢鲍勃的来信。我没有意识到这一点。我在 Python2.7 和 3.3 中测试了{}-version,我在这两个版本中都运行良好。
  • 第一个选项只打印字母b。
  • @gineraso 你用的是哪个python版本?
  • Python 版本为 Python 2.6.6
【解决方案3】:

您需要将\n 放在引号中。它也是一个字符串字面量,因此您需要将first"\n"second 连接在一起以生成一个要打印的字符串。

print first + "\n" + second

【讨论】:

    【解决方案4】:

    您忘记了firstsecond 之间的+。要修复您的示例,请执行以下操作:

    `print first + '\n' + second`
    

    或者你可以使用如下格式字符串:(我的偏好)

    first = 'foo'
    second = 'bar'
    print '%s\n%s' % (first, second)
    
    # foo
    # bar
    

    【讨论】:

    • 这只会将第二个变量打印到屏幕上。
    • 打印出first,仅此而已。是空白吗?可能没有任何东西被输入标准输出..
    • 打印出第一个变量:['Red Hat Enterprise Linux Server release 6.5 (Santiago)\n']
    • 呃,它可能会做一些奇怪的事情,因为它是一个列表。试试print '%s\n%s' % (first[0], second)
    • 仅将第二个变量打印到屏幕:['Red Hat Enterprise Linux Server release 6.2 (Santiago)\n']
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多