Python:Windows上的编码问题(散景图库) [英] Python: Trouble with encoding on Windows (Bokeh plotting library)

查看:29
本文介绍了Python:Windows上的编码问题(散景图库)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从64上的 Bokeh教程中复制最简单的示例.位版本的Windows计算机以及Python 3.3.0.

I am trying to reproduce the simplest examples from the Bokeh tutorial, on a 64-bit Windows machine with Python 3.3.0.

这是完整的代码

import pandas as pd
import numpy as np
import matplotlib.pyplot as mpl

# NOTE need this import as output_file was not getting imported into the 
#     global namespace
import bokeh.plotting as bkp
from bokeh.plotting import *

# Skip the first point because it can be troublesome
theta = np.linspace(0, 8*np.pi, 10000)[1:]

# Compute the radial coordinates for some different spirals
lituus = theta**(-1/2)          # lituus
golden = np.exp(0.306349*theta) # golden
arch   = theta                  # Archimedean
fermat = theta**(1/2)           # Fermat's

# Now compute the X and Y coordinates (polar mappers planned for Bokeh later)
golden_x = golden*np.cos(theta)
golden_y = golden*np.sin(theta)
lituus_x = lituus*np.cos(theta)
lituus_y = lituus*np.sin(theta)
arch_x   = arch*np.cos(theta)
arch_y   = arch*np.sin(theta)
fermat_x = fermat*np.cos(theta)
fermat_y = fermat*np.sin(theta)

# output to static HTML file
bkp.output_file("lines.html")

# Plot the Archimedean spiral using the `line` renderer. Note how we set the
# color, line thickness, title, and legend value.
line(arch_x, arch_y, color="red", line_width=2, title="Archimean", legend="Archimedean")

这给了我以下错误:

Traceback (most recent call last):
  File "F:\programming\python\python64\python33\lib\site-packages\IPython\core\interactiveshell.py", line 2732, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-1-00be3b4eba05>", line 1, in <module>
    bkp.line(arch_x, arch_y, color="red", line_width=2, title="Archimean", legend="Archimedean")
  File "F:\programming\python\python64\python33\lib\site-packages\bokeh\plotting.py", line 318, in wrapper
    save()
  File "F:\programming\python\python64\python33\lib\site-packages\bokeh\plotting.py", line 284, in save
    f.write(html)
  File "F:\programming\python\python64\python33\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 1831286-1831289: character maps to <undefined>

我知道这与Python用于写入输出文件的编码有关,但是对设置输出文件的编码或Python用于写入的编码的了解还不够解决这个问题.帮助表示赞赏.

I understand that this has something to do with the encoding that Python is using to write to the output file, but don't know enough about setting the encoding of the output file or the encoding that is being used by Python to write out to fix this. Help appreciated.

我试图实施此处给出的建议,以始终通过流编写器传递stdout输出:

I tried to implement the advice given here, to always pass stdout output through a streamwriter:

if sys.stdout.encoding != 'UTF-8':
    sys.stdout = codecs.getwriter('utf-8')(sys.stdout.buffer, 'strict')
if sys.stderr.encoding != 'UTF-8':
    sys.stderr = codecs.getwriter('utf-8')(sys.stderr.buffer, 'strict')

,但是某些界面似乎已更改,并且没有sys.stdout.encoding变量.

but some of the interface appears to have changed, and there is no sys.stdout.encoding variable.

Traceback (most recent call last):
  File "F:\programming\python\python64\python33\lib\site-packages\IPython\core\interactiveshell.py", line 2732, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-1-e12310bc7a07>", line 1, in <module>
    if sys.stdout.encoding != 'UTF-8':
  File "F:\programming\python\python64\python33\lib\codecs.py", line 387, in __getattr__
    return getattr(self.stream, name)
AttributeError: '_io.FileIO' object has no attribute 'encoding'

推荐答案

我打开了一个跟踪此问题的问题: https://github.com/ContinuumIO/bokeh/issues/682

I have opened an issue to track this problem: https://github.com/ContinuumIO/bokeh/issues/682

正如您与eryksun讨论的那样,它似乎很容易修复.

As you have discussed with eryksun, it seems easily fixable.

我会在这里为您提供最新信息,但是如果您想参与此问题,我们将非常欢迎.

I will keep you updated here, but if you want to participate in the issue, you are very welcome.

欢呼

这篇关于Python:Windows上的编码问题(散景图库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆