簡體   English   中英

不輸出到端子時的編碼錯誤

[英]Encoding error when not outputting to terminal

我想知道為什么捕獲(管道)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\")))"'

這是我第一次遇到這種行為。 我的單線紙是否以某種方式不處理管道輸出?

僅需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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM