【问题标题】:How to clean python print() output on Windows for piping?如何在 Windows 上为管道清理 python print() 输出?
【发布时间】:2021-05-07 08:27:01
【问题描述】:

我正在尝试使用 xargs 管道 python stdout 输出以 curl 并在此任务中失败。

curl 可以接受来自 echo 的输入

λ echo https://google.com | xargs curl
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://www.google.com/">here</A>.
</BODY></HTML>

但是当从 python 打印相同的内容时,xargs curl 输入在 Windows(使用 cmder)上变得无效。

这在 WSL 中运行良好:

python3 -c"print('https://google.com')" | xargs curl
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://www.google.com/">here</A>.
</BODY></HTML>

但在 Windows 上可能会有一些换行符/换行符干扰:

λ python -c"print('https://google.com')" | xargs curl
curl: (3) URL using bad/illegal format or missing URL

如何在 Windows 上清理 python 打印输出以使上述命令正常工作?

【问题讨论】:

    标签: python shell xargs


    【解决方案1】:

    应该猜到了!

    python -c"print('https://google.com', end='')" | xargs curl
    

    How to print without newline or space?

    我太喜欢为 xargs 找到一些可以完成所有工作的论据。

    【讨论】:

      猜你喜欢
      • 2020-07-28
      • 2015-12-09
      • 1970-01-01
      • 2019-02-15
      • 2012-01-20
      • 1970-01-01
      • 2010-10-05
      • 2019-10-05
      • 1970-01-01
      相关资源
      最近更新 更多