简体   繁体   English

读取或读取线的Python自定义分隔符

[英]Python custom delimiter for read or readline

I am interacting with a subprocess and trying to detect when it is ready for my input. 我正在与subprocess交互并尝试检测它何时准备好输入。 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. 我遇到的问题是read或readline函数依赖于行尾的'\\ n'分隔符,或者产生EOF。 Since this subprocess never exits, there is no EOF in the file like object. 由于此subprocess永不退出,因此文件中没有EOF ,如对象。 Since the keyword that I want to trigger off of does not contain that delimiter the read and readline functions never yield. 由于我想要触发的关键字不包含该分隔符,因此read和readline函数永远不会产生。 For example: 例如:

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

Since this process never exits, the read or read line never see an EOF or \\n that it requires to yield. 由于此过程永远不会退出,因此读取或读取行永远不会看到它需要产生的EOF\\n

Is there a way to read this file like object and to set a custom delimiter to input> ? 有没有办法像对象一样读取此文件并将自定义分隔符设置为input>

You can implement your own readlines function and choose the delimiter yourself: 您可以实现自己的readlines函数并自己选择分隔符:

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

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. 遗憾的是,由于Python默认缓冲策略,如果您正在调用的进程不提供大量数据,则无法获取大量数据,但您始终可以将chunk_size设置为1 ,然后读取输入字符按性格。 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...

And it should capture everything up to > from your subprocesses' STDOUT. 它应该抓住一切都交给>从你的子进程标准输出。 You can also use multiple characters as separators in this version. 您还可以在此版本中使用多个字符作为分隔符。

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

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