【问题标题】:Encoding error when not outputting to terminal不输出到终端时出现编码错误
【发布时间】:2018-03-17 12:33:28
【问题描述】:

我想知道为什么捕获(管道)标准输出会导致一个空文件,而 不捕获它会导致正常输出。输出到终端时我没有收到编码错误。仅在管道输出时。

输出到管道而不是终端时编码是否会改变?也许终端可以发出支持的编码信号,而管道默认为 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\")))"'

这是我第一次遇到这种行为。我的单线是否以某种方式无法处理管道输出?

【问题讨论】:

  • 在运行之前尝试取消设置LC_ALL...unset LC_ALL; curl ...
  • @MarkSetchell 同样的错误。
  • 你的 python 命令是运行 python 2 还是 python 3?文本编码存在问题,但在 python 3 中添加了一些有用的默认值,因此也许您不必更改程序。
  • @ArndtJonasson 你说得对,安装 Python 3 解决了这个问题。所以谢谢你。但是 why python 2 的行为问题仍然存在:-)
  • 我不是专家,但谷歌搜索“python unicode”似乎会带来有用的信息。

标签: python shell pipe pipeline


【解决方案1】:

只需我的 2 美分,因为输入数据已正确解码 (.decode(\"utf-8\")),输出数据也需要编码 (.encode(\"utf-8\"))。所以你的一个班轮的工作版本应该如下。 why的问题需要很长时间来研究。

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

【讨论】:

  • 谢谢!我之前尝试时将编码添加到错误的部分。但是为什么它在不管道时起作用?输出到管道而不是终端时编码是否会改变?也许终端可以发出支持的编码信号,而管道默认为 ASCII?
  • 它适用于管道。我尝试了curl -s 'https://www.sunwind.no/Outlet/' | html2text | grep __,发现 grep 可以处理来自 html2txt 的输出。我认为您对终端可以支持 html2txt 的编码是正确的。我也认为管道只是字节流。并且管道中的程序需要对字节流有相同的理解(编码/解码方法)。
  • 对不起,在网上写作很难。 'it' 指的是原始版本,没有对结果流进行显式编码。我知道您的版本适用于管道,而我的版本不适用。
  • 对不起,我错误地通过 grep 管道而不使用原始版本。祝你有美好的一天。 :)
猜你喜欢
  • 2019-07-22
  • 2021-11-27
  • 2019-05-08
  • 1970-01-01
  • 1970-01-01
  • 2017-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多