不输出到终端时编码错误

2024-10-05 10:42:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道为什么捕获(管道)stdout会导致一个空文件,而不捕获它会导致正常的输出。当输出到终端时,我没有得到编码错误。只有在管道输出时。你知道吗

当输出到管道而不是终端时,编码是否更改?也许终端可以发送支持的编码信号,而管道默认为ASCII?你知道吗

管道输出

$ curl -s 'https://www.sunwind.no/Outlet/'  | html2text | wc
Traceback (most recent call last):
  File "<string>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe5' in position 212: ordinal not in range(128)
      0       0       0

未捕获输出

curl -s 'https://www.sunwind.no/Outlet/' | html2text
  * [Forsiden](https://www.sunwind.no/)
  * [Logg inn](/login/)
  * [Registrer meg](https://www.sunwind.no/register/)

[![](https://www.sunwind.no/images/flag/NORW0001.GIF)](https://www.sunwind.no
"Klikk her for å gå til forsiden")
[![](https://www.sunwind.no/images/flag/SWDN0001.GIF)](https://www.sunwind.se
"Klikk her for å gå til Sunwinds svenske side")
[![](https://www.sunwind.no/images/flag/FINL0001.GIF)](https://www.sunwind.fi
"Klikk her for å gå til Sunwinds finske side")
[![](https://www.sunwind.no/images/flag/DENM0001.GIF)](https://www.sunwind.no/page/?pid=132
"Klikk her for å gå til Sunwinds danske side")
[![](https://www.sunwind.no/images/flag/UK0001.GIF)](https://www.sunwind.no/en/
"Klikk her for å gå til engelsk side")

[ ![](https://www.sunwind.no/images/Logo.png)](https://www.sunwind.no/)

![](https://www.sunwind.no/images/Enjoy_Brun_logo_text.png)

  * __ 0 **kr 0,-**

Nylig lagt til i handlevognen

[Gå til handlekurven](https://www.sunwind.no/account/basket/)

[ Sunwind.no](https://www.sunwind.no/)

  * Alle produkter __

##### **[KJØKKEN OG GASS](/product/content/show/?cap=7&KJOKKEN-OG-GASS) **

    * [__Gasskomfyr](https://www.sunwind.no/product/category/?cap=13)
    * [__Innbyggingsovn gass](https://www.sunwind.no/product/category/?cap=14)
    * [__Gasstopp](https://www.sunwind.no/product/category/?cap=15)
    * [__Gasskjøleskap](https://www.sunwind.no/product/category/?cap=63)
    * [__Kjøleskap 12 volt](https://www.sunwind.no/product/category/?cap=148)
    * [__Kjøle- og fryseboks](https://www.sunwind.no/product/category/?cap=144)
    * [__Kjøkkenvifte](https://www.sunwind.no/product/category/?cap=65)
    * [__Tilbehør og turutstyr](https://www.sunwind.no/product/category/?cap=97)
    * [__Gassutstyr og monteringsmateriell](https://www.sunwind.no/product/category/?cap=20)


etc

html2text别名是一行python命令:

alias html2text='python -c "import sys,html2text;sys.stdout.write(html2text.html2text(sys.stdin.read().decode(\"utf-8\")))"'

这是我第一次遇到这种行为。我的单行程序不处理管道输出吗?你知道吗


Tags: nohttps管道wwwproductgifflagcap
2条回答

只要我的2美分,因为输入数据被正确解码(.decode(\"utf-8\")),输出数据也需要被编码(.encode(\"utf-8\"))。因此,一行代码的工作版本应该如下所示。这个问题需要很长时间来研究。你知道吗

alias html2text='python2 -c "import sys,html2text;sys.stdout.write(html2text.html2text(sys.stdin.read().decode(\"utf-8\")).encode(\"utf-8\"))"'
curl -s 'https://www.sunwind.no/Outlet/'  | html2text | wc
    764    1839   29329

只要我的2美分,因为输入数据被正确解码(.decode(\"utf-8\")),输出数据也需要被编码(.encode(\"utf-8\"))。因此,一行代码的工作版本应该如下所示。这个问题需要很长时间来研究。你知道吗

alias html2text='python2 -c "import sys,html2text;sys.stdout.write(html2text.html2text(sys.stdin.read().decode(\"utf-8\")).encode(\"utf-8\"))"'
curl -s 'https://www.sunwind.no/Outlet/'  | html2text | wc
    764    1839   29329

相关问题 更多 >

    热门问题