简体   繁体   English

在 output 之前打印 pexpect 是带有 \r\n 而不是实际 CR/LF output 的单行

[英]printing pexpect before output is a single line with \r\n instead of actual CR/LF output

I will apologize in advance that I am new to Python3 and stuck on understanding the actual output as opposed to what I am expecting to receive.我会提前道歉,我是 Python3 的新手,坚持理解实际的 output 而不是我期望收到的。

Here is my code:这是我的代码:

    import pexpect
    ping = pexpect.spawn('ping -c 5 8.8.8.8')
    result = ping.expect([pexpect.EOF, pexpect.TIMEOUT])
    print(ping.before)

The actual output is a single line starting with "b" and \r\n added instead of the actual lines wrapping.实际的 output 是以“b”开头的单行并添加了 \r\n 而不是实际的换行。

I expect this:我期望这个:

    PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
    64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=78.1 ms
    64 bytes from 8.8.8.8: icmp_seq=2 ttl=128 time=76.5 ms
    64 bytes from 8.8.8.8: icmp_seq=3 ttl=128 time=76.2 ms
    64 bytes from 8.8.8.8: icmp_seq=4 ttl=128 time=76.8 ms
    64 bytes from 8.8.8.8: icmp_seq=5 ttl=128 time=77.0 ms

But instead my output is one giant line:但相反,我的 output 是一条巨大的线路:

    b'PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.\r\n64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=78.1 ms\r\n64 bytes from 8.8.8.8: icmp_seq=2 ttl=128 time=76.5 ms\r\n64 bytes from 8.8.8.8: icmp_seq=3 ttl=128 time=76.2 ms\r\n64 bytes from 8.8.8.8: icmp_seq=4 ttl=128 time=76.8 ms\r\n64 bytes from 8.8.8.8: icmp_seq=5 ttl=128 time=77.0 ms\r\n\r\n--- 8.8.8.8 ping statistics ---\r\n5 packets transmitted, 5 received, 0% packet loss, time 4004ms\r\nrtt min/avg/max/mdev = 76.214/76.951/78.149/0.683 ms\r\n'

Does anyone have any idea?有人有什么主意吗?

The problem here is that your output is in the form of a byte-string -- also known as the bytes type in Python.这里的问题是您的 output 是字节串的形式——也称为 Python 中的bytes类型。 This data type is often encountered when interacting with data/content that originated outside of the application.在与源自应用程序外部的数据/内容交互时,经常会遇到这种数据类型。 Essentially, Python doesn't know definitively what encoding standard to use while attempting to interpret the data in a buffer.本质上,Python 在尝试解释缓冲区中的数据时并不确定使用什么编码标准。 Rather than attempting to guess at it, Python will instead leave it to the developer to decide how to interpret the raw data contained in the byte-string. Python 不会试图猜测它,而是让开发人员决定如何解释字节字符串中包含的原始数据。 This allows compatibility with character encoding systems that use more than one byte per character (eg, UTF-16 and UTF-32 ).这允许与每个字符使用多于一个字节的字符编码系统(例如UTF-16UTF-32 )兼容。 However, this also means that developers have to convert the data from bytes to str before they can interact with such data in their applications.然而,这也意味着开发人员必须先将数据从bytes转换为str ,然后才能在应用程序中与此类数据进行交互。

In your case, you should likely interpret the data as UTF-8.在您的情况下,您可能应该将数据解释为 UTF-8。 To do this, you would replace your current print line with:为此,您可以将当前的打印行替换为:

print(ping.before.decode('utf-8', 'ignore'))

To clarify that command, we are interacting with the bytes object to call the decode(...) method, and we are providing the decode method first with the character encoding codec that we would like the data to be interpreted as, and second with the name of the error-handling approach we want to use.为了澄清该命令,我们正在与字节object 交互以调用decode(...)方法,我们首先为 decode 方法提供我们希望数据被解释为的字符编码编解码器,然后提供我们要使用的错误处理方法的名称。 The error-handling approach for a decode has a slew of possible values, but two common approaches are ignore and strict , with strict being the default handler.解码的错误处理方法有许多可能的值,但两种常见的方法是ignorestrict ,其中strict是默认处理程序。

The ignore handler essentially tells the decode method that if it comes across a value which it cannot interpret/decode, it should silently discard that value and continue decoding the remaining values.忽略处理程序本质上告诉解码方法,如果遇到无法解释/解码的值,它应该默默地丢弃该值并继续解码剩余的值。

On the other hand, strict tells the decode method that it should halt all processing and raise an error (specifically the UnicodeError ).另一方面, strict告诉 decode 方法它应该停止所有处理并引发错误(特别是UnicodeError )。

You can read more on encode/decode error-handling options on the Python documentation page for Codec Error Handling .您可以在 Python 文档页面上阅读有关编解码器错误处理的更多信息。

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

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