当前位置: 首页 > 面试题库 >

将python的stdout重定向到文件失败,并出现UnicodeEncodeError

高高雅
2023-03-14
问题内容

我有一个连接到Twitter
Firehose并将其向下游发送数据进行处理的python脚本。在此之前,它可以正常工作,但是现在,我正在尝试仅获取文本正文。(这不是我应该如何从Twitter提取数据或如何编码/解码ascii字符的问题)。因此,当我像这样直接启动脚本时:

python -u fetch_script.py

它工作正常,我可以看到消息正在显示在屏幕上。例如:

root@domU-xx-xx-xx-xx:/usr/local/streaming# python -u fetch_script.py 
Cuz I'm checking you out >on Facebook<
RT @SearchlightNV: #BarryLies has crapped on all honest patriotic hard-working citizens in the USA but his abuse of WWII Vets is sick #2A…
"Why do men chase after women? Because they fear death."~Moonstruck
RT @SearchlightNV: #BarryLieshas crapped on all honest patriotic hard-working citizens in the USA but his abuse of WWII Vets is sick #2A…
Never let anyone tell you not to chase your dreams. My sister came home crying today, because someone told her she's not good enough.
"I can't even ask anyone out on a date because if it doesn't end up in a high speed chase, I get bored."
RT @ColIegeStudent: Double-checking the attendance policy while still in bed
Well I just handed my life savings to ya.. #trustingyou #abouttomakebankkkkk
Zillow $Z and Redfin useless to Wells Fargo Home Mortgage, $WFC, and FannieMae $FNM. Sale history LTV now 48%, $360 appraisal fee 4 no PMI.
The latest Dump and Chase Podcast http://t.co/viaRSA9W3i check it out and subscribe on iTunes, or your favorite android app #Isles

但是,如果我尝试将它们输出到文件中,如下所示:

python -u fetch_script.py >fetch_output.txt

愚蠢地抛出我和错误

root@domU-xx-xx-xx-xx:/usr/local/streaming# python -u fetch_script.py >fetch_output.txt
ERROR:tornado.application:Uncaught exception, closing connection.
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/tornado/iostream.py", line 341, in wrapper
    callback(*args)
  File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 331, in wrapped
    raise_exc_info(exc)
  File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 302, in wrapped
    ret = fn(*args, **kwargs)
  File "/usr/local/streaming/twitter-stream.py", line 203, in parse_json
    self.parse_response(response)
  File "/usr/local/streaming/twitter-stream.py", line 226, in parse_response
    self._callback(response)
  File "fetch_script.py", line 57, in callback
    print msg['text']
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2026' in position 139: ordinal not in range(128)
ERROR:tornado.application:Exception in callback <functools.partial object at 0x187c2b8>
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/tornado/ioloop.py", line 458, in _run_callback
    callback()
  File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 331, in wrapped
    raise_exc_info(exc)
  File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 302, in wrapped
    ret = fn(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/tornado/iostream.py", line 341, in wrapper
    callback(*args)
  File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 331, in wrapped
    raise_exc_info(exc)
  File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 302, in wrapped
    ret = fn(*args, **kwargs)
  File "/usr/local/streaming/twitter-stream.py", line 203, in parse_json
    self.parse_response(response)
  File "/usr/local/streaming/twitter-stream.py", line 226, in parse_response
    self._callback(response)
  File "fetch_script.py", line 57, in callback
    print msg['text']
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2026' in position 139: ordinal not in range(128)

更多内容:

callback函数发生错误:

def callback(self, message):
        if message:
            msg = message
            msg_props = pika.BasicProperties()
            msg_props.content_type = 'application/text'
            msg_props.delivery_mode = 2
            #print self.count
            print msg['text']
            #self.count += 1
            ...

但是, 如果我撤职 ['text']并且能够活下去,那么print msg这两种情况都像魅力一样。


问题答案:

既然还没有人跳进来,这就是我的镜头。在写入控制台时,Python会设置stdout的编码,但在写入文件时,Python不会设置。该脚本显示了问题

import sys

msg = {'text':u'\2026'}
sys.stderr.write('default encoding: %s\n' % sys.stdout.encoding)
print msg['text']

运行显示错误

$ python bad.py>/tmp/xxx
default encoding: None
Traceback (most recent call last):
  File "fix.py", line 5, in <module>
    print msg['text']
UnicodeEncodeError: 'ascii' codec can't encode character u'\x82' in position 0: ordinal not in range(128)

添加编码

import sys

msg = {'text':u'\2026'}
sys.stderr.write('default encoding: %s\n' % sys.stdout.encoding)
encoding = sys.stdout.encoding or 'utf-8'
print msg['text'].encode(encoding)

问题解决了

$ python good.py >/tmp/xxx
default encoding: None
$ cat /tmp/xxx
6


 类似资料:
  • 问题内容: 如何在Python中将stdout重定向到任意文件? 当从ssh会话中启动运行了很长时间的Python脚本(例如,Web应用程序)并进行背景调整,并且ssh会话关闭时,该应用程序将在尝试写入stdout时引发并失败。我需要找到一种方法来使应用程序和模块输出到文件而不是stdout,以防止由于而导致失败。当前,我使用将输出重定向到文件,并且可以完成工作,但是我想知道是否有一种出于好奇而无

  • 问题内容: 我在cyberciti.biz的评论中看到了这个有趣的问题。 我发现我什至找不到在sh的单行命令中执行此操作的灵活方法。 到目前为止,我对解决方案的想法是: 但是您会看到,这不是同步的,而且致命的是,它是如此丑陋。 欢迎与您分享这个想法。:) 问题答案: 你要 这里的顺序很重要。假设stdin(fd 0),stdout(fd 1)和stderr(fd 2)最初都连接到tty,因此 首先

  • 问题内容: 我想从Groovy程序中执行foo.bat,并将生成的进程的输出重定向到stdout。Java或Groovy代码示例都可以。 foo.bat可能需要花费几分钟才能运行并生成大量输出,因此我希望在生成后立即查看输出,而不是必须等到该过程完成之后才能立即查看所有输出。 问题答案: 它使用一个类读取执行的程序生成的所有输出,并将其显示在其自己的stdout中。

  • 问题内容: 我正在尝试将函数的标准输出重定向到tkinter文本小部件。我遇到的问题是,它会将每一行写到一个新窗口中,而不是将所有内容都列出在一个窗口中。该函数扫描目录并列出任何0k文件。如果没有文件为0k,则将其打印出来。因此,问题在于,如果目录中有30个0k文件,它将打开30个窗口,每个窗口只有一行。现在,我知道问题出在哪里。如果您查看我的功能代码,我会告诉您: 我知道,每次os.stat看到

  • 我想将stdout和stderr重定向到一个文件,同时保留输出顺序,然后还将stderr显示到屏幕上。我看到很多问题讨论它: https://unix.stackexchange.com/questions/9646/show-only-stderr-on-screen-but-write-both-stdout-and-stderr-to-file https://unix.stackexcha

  • 我这里有一个非常奇怪的用例,我正试图为我的学生编写一些简单的程序,帮助他们学习python。为了让它工作,我在TKinter框架中嵌入了一个PyGame窗口,我需要重定向stdout以更改PyGame窗口中的某些内容。我有重定向工作,如果我重定向到一个文件,它的工作很好,但如果我试图改变文本,它不工作。我将一个字符串硬编码到PyGame文本更改代码中,这是可行的,但由于某些原因,它无法与重定向文本