[英]Python custom readline function misses some lines
我正在嘗試編寫一個readline函數,該函數從ai / o設備請求固定數量的字節,並緩沖接收到的數據並返回一行。
該設備沒有它自己的readline()方法,只有一個recv(bytes)方法,因此沒有我的功能。
在這種情況下,recv()函數僅模擬I / O設備,純粹用於測試/調試。
問題是預期輸出中應該缺少“ a ”和“ of ”:
peter piper picked a peck of pickled peppers
我不知道為什么。
buff是包含完整行的數組,而xbuff保存部分行。
str = ''
buff = [];
xbuff = b''
data = b"peter\r\npiper\r\npicked\r\na\r\npeck\r\nof\r\npickled\r\npeppers"
def readline():
global buff,xbuff
raw = recv(10)
buff = (xbuff + raw).splitlines()
xbuff = b''
if len(raw) == 10 and not raw.endswith(b'\r\n'):
xbuff = buff.pop()
if len(buff) > 0:
line = buff.pop(0)
return line
return b''
def recv(chrs):
global data
out = data[:chrs]
data = data[chrs:]
return out
while True:
line = readline()
if line:
str += " "+line.decode()
else:
print(str)
break
peter piper picked peck pickled peppers
糟糕,需要附加到緩沖區而不是替換緩沖區
buff += (xbuff + raw).splitlines()
但這是最pythonic /最有效的方法嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.