简体   繁体   English

Python:将元组转换为字符串

[英]Python: Converting a tuple to a string

Given this : 鉴于这种 :

import os
import subprocess

def check_server():

    cl = subprocess.Popen(["nmap","10.7.1.71"], stdout=subprocess.PIPE)
    result = cl.communicate()
    print result

check_server()

check_server() returns this tuple: check_server()返回此元组:

('\nStarting Nmap 4.53 ( http://insecure.org ) at 2010-04-07 07:26 EDT\nInteresting ports on 10.7.1.71:\nNot shown: 1711 closed ports\nPORT   STATE SERVICE\n21/tcp open  ftp\n22/tcp open  ssh\n80/tcp open  http\n\nNmap done: 1 IP address (1 host up) scanned in 0.293 seconds\n', None)

Changing the second line in the method to 将方法中的第二行更改为

result, err = cl.communicate()

results in check_server() returning : 导致check_server()返回:

Starting Nmap 4.53 ( http://insecure.org ) at 2010-04-07 07:27 EDT
Interesting ports on 10.7.1.71:
Not shown: 1711 closed ports
PORT   STATE SERVICE
21/tcp open  ftp
22/tcp open  ssh
80/tcp open  http

Nmap done: 1 IP address (1 host up) scanned in 0.319 seconds

Looks to be the case that the tuple is converted to a string, and the \\n's are being stripped.... but how? 看起来元组已转换为字符串,并且\\ n被剥离了。

cl.communicate() is still returning a tuple. cl.communicate()仍返回一个元组。 The assignment result, err = ... has the effect of unpacking the tuple into the variables result (a string) and err (an integer). 赋值result, err = ...的作用是将元组解压缩为变量result (字符串)和err (整数)。

When you print the tuple, it uses the repr(...) of each element, but when you print the string, it just prints the string, hence the absence of delimiters and \\n s. 当您打印元组时,它使用每个元素的repr(...) ,但是当您打印字符串时,它仅打印字符串,因此没有分隔符和\\n

In both cases cl.communicate() returns a two-element tuple (stdout and stderr of the command if both stdout and stderr are redirected to a PIPE). 在这两种情况下, cl.communicate()返回一个由两个元素组成的元组(如果将stdout和stderr都重定向到PIPE,则返回命令的stdout和stderr)。 In first case you assign this tuple to the result variable. 在第一种情况下,您将此元组分配给result变量。 When you print it you get a text representation of the tuple. 打印时,将获得元组的文本表示。 Special characters, like end-of-line are escaped (to '\\n') in such representation. 在这种表示形式中,特殊字符(例如行尾)被转义(到'\\ n')。

In the second case you unpack the tuple. 在第二种情况下,请打开元组的包装。 This means: you assign the first element of the tuple to result and the second to err . 这意味着:您将元组的第一个元素分配给result ,将第二个元素分配给err As the result is already a string it is printed with no escaping – end-of-line characters are displayed as line breaks and not '\\n'. 由于result已经是一个字符串,因此没有转义就可以打印-换行符显示为换行符而不是'\\ n'。

print is clever. print很聪明。 If you pass it a string, it just prints it. 如果将字符串传递给它,它只会打印出来。 If you pass it something else (a tuple in this example) then it prints it's representation (as returned by repr() ). 如果您将其传递给其他对象(在此示例中为元组),则它将打印其表示形式(由repr()返回)。

The representation of a string has \\n where the string contains newlines. 字符串的表示形式为\\n ,其中字符串包含换行符。 The representation also has quotes around it. 该表示形式周围也有引号。

Try changing your print statement to print repr(result) and you'll see. 尝试将print语句更改为print repr(result) ,您会看到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM