用于读取或读取行的Python自定义分隔符 [英] Python custom delimiter for read or readline

查看:161
本文介绍了用于读取或读取行的Python自定义分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与子进程进行交互,并尝试检测何时可以为我的输入做好准备。我遇到的问题是read或 readline 函数依赖于行尾的'n'分隔符,或者依靠EOF来产生。由于此子进程永不退出,因此像对象文件中没有 EOF 。由于我要触发的关键字不包含该分隔符,因此read和 readline 函数永远不会产生。例如:

I am interacting with a subprocess and trying to detect when it is ready for my input. The problem that I am having is that the read or readline functions rely on the '\n' delimiter at the end of the line, or an EOF to yield. Since this subprocess never exits, there is no EOF in the file like object. Since the keyword that I want to trigger off of does not contain that delimiter the read and readline functions never yield. For example:

'Doing something\n'
'Doing something else\n'
'input>'

由于此过程永远不会退出,因此读取或读取行永远不会看到<$ c需要产生的$ c> EOF 或 \n

Since this process never exits, the read or read line never see an EOF or \n that it requires to yield.

是有没有办法像对象一样读取此文件并将自定义定界符设置为 input>

Is there a way to read this file like object and to set a custom delimiter to input>?

推荐答案

您可以实现自己的 readlines 函数并自行选择定界符:

You can implement your own readlines function and choose the delimiter yourself:

def custom_readlines(handle, line_separator="\n", chunk_size=64):
    buf = ""  # storage buffer
    while not handle.closed:  # while our handle is open
        data = handle.read(chunk_size)  # read `chunk_size` sized data from the passed handle
        if not data:  # no more data...
            break  # break away...
        buf += data  # add the collected data to the internal buffer
        if line_separator in buf:  # we've encountered a separator
            chunks = buf.split(line_separator)
            buf = chunks.pop()  # keep the last entry in our buffer
            for chunk in chunks:  # yield the rest
                yield chunk + line_separator
    if buf:
        yield buf  # return the last buffer if any

不幸的是,由于Python默认的缓冲策略,如果您所调用的进程未提供大量数据,则您将无法捕获大量数据,但是您始终可以求助于将 chunk_size 设置为 1 ,然后逐个读取输入的字符。因此,对于您的示例,您所需要做的就是:

Unfortunately, due to Python default buffering policies you won't be able to grab large swaths of data if they are not provided by the process you're calling, but you can always resort to setting the chunk_size to 1 and then read the input character by character. So, for your example, all you need to do is:

import subprocess

proc = subprocess.Popen(["your", "subprocess", "command"], stdout=subprocess.PIPE)

while chunk in custom_readlines(proc.stdout, ">", 1):
    print(chunk)
    # do whatever you want here...

应该从子流程的STDOUT中捕获所有> 的内容。在此版本中,您还可以使用多个字符作为分隔符。

And it should capture everything up to > from your subprocesses' STDOUT. You can also use multiple characters as separators in this version.

这篇关于用于读取或读取行的Python自定义分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆