【问题标题】:Python stdin readline over 4096 bytes in linux x64?linux x64中的Python stdin readline超过4096字节?
【发布时间】:2020-01-26 20:36:06
【问题描述】:

我有一个长度超过 4096 字节的输入行进入 Python 中的标准输入。代码简单地说:

while True:
    task_json = sys.stdin.readline()
    task_json = json.loads(task_json.encode('utf-8', 'surrogateescape').decode('utf-8- sig', 'surrogateescape'))

在 4096 字节处截断行。有人知道解决方案吗? 我在 ubuntu 16.04 x64 上使用 python3.6。我尝试使用 -u 标志 - 不起作用。我的原始大小是 9k 字节。我无法将此行拆分为 \n 并进行修改,我只需要来自控制台的标准输入。 我可以配置控制台模式,切换到非佳能模式吗?

【问题讨论】:

  • 无法在 Python 3.7 和 Bash 上重现。该脚本应有 9k 字节的输入。
  • 你是如何输入数据的?使用重定向还是粘贴到终端?如果是后者,可能是终端的一些限制。
  • 在 python3.7 中,您可以尝试重新配置 stdin 以更改换行符处理以查看问题是否与通用换行符有关...

标签: python python-3.x stdin


【解决方案1】:

需要禁用 TTY 的规范模式。

def getline(prompt=""):
  fd = sys.stdin.fileno()
  old = termios.tcgetattr(fd)
  new = termios.tcgetattr(fd)
  new[3] = new[3] & ~termios.ICANON
  try:
    termios.tcsetattr(fd, termios.TCSADRAIN, new)
    line = input(prompt)
  finally:
    termios.tcsetattr(fd, termios.TCSADRAIN, old)
  return line

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2018-03-28
    • 1970-01-01
    相关资源
    最近更新 更多