【问题标题】:Output of proc.communicate() does not format newlines in django pythonproc.communicate() 的输出不会在 django python 中格式化换行符
【发布时间】:2010-08-28 22:32:44
【问题描述】:

我有一个子进程使用通信来获取输出并将其保存到我的数据库中:

  p = Popen([str(pre_sync), '-avu', str(src), str(dest)], stdout=PIPE)
  syncoutput = p.communicate()
  check.log = syncoutput

一切正常,但从通信的输出看起来像这样:

('sending incremental file list\n\nsent 89 bytes  received 13 bytes  204.00 bytes/sec\ntotal size is 25  speedup is 0.25\n', None)

全部在一行中并插入“\n”。有没有办法让它在新行中打印每一行?提前致谢。

【问题讨论】:

    标签: python django django-views popen


    【解决方案1】:
    syncoutput,sync_error = p.communicate()
    print(syncoutput)
    

    p.communicate() 返回一个 2 元组,由 stdout 和 stderr 的输出组成。打印 2 元组时,您会看到 \n 字符。当您打印字符串(新的syncoutput)时,您将获得格式化文本。

    【讨论】:

    • @dura,一定要接受这个答案(检查A左侧的复选标记形图标)-“谢谢很便宜,但接受问题”是一部分SO的基本礼仪!-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-26
    • 2012-03-01
    • 2017-08-11
    • 2023-04-04
    • 1970-01-01
    相关资源
    最近更新 更多