简体   繁体   English

Python:Windows上的编码问题(Bokeh绘图库)

[英]Python: Trouble with encoding on Windows (Bokeh plotting library)

I am trying to reproduce the simplest examples from the Bokeh tutorial , on a 64-bit Windows machine with Python 3.3.0. 我试图在使用Python 3.3.0的64位Windows机器上重现Bokeh教程中最简单的例子。

Here is the code in its entirety 这是完整的代码

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")

This gives me the following error: 这给了我以下错误:

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>

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. 我知道这与Python用来写入输出文件的编码有关,但是对于设置输出文件的编码或者Python用来写出修复的编码知之甚少这个。 Help appreciated. 帮助赞赏。

Edit: 编辑:

I tried to implement the advice given here , to always pass stdout output through a streamwriter: 我试图实现这里给出的建议,总是通过一个streamwriter传递stdout输出:

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')

but some of the interface appears to have changed, and there is no sys.stdout.encoding variable. 但是有些界面似乎已经改变,并且没有sys.stdout.encoding变量。

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'

I have opened an issue to track this problem: https://github.com/ContinuumIO/bokeh/issues/682 我已经开了一个问题来跟踪这个问题: https//github.com/ContinuumIO/bokeh/issues/682

As you have discussed with eryksun, it seems easily fixable. 正如您与eryksun讨论的那样,它似乎很容易解决。

I will keep you updated here, but if you want to participate in the issue, you are very welcome. 我会在这里向您通报,但如果您想参与此问题,我们非常欢迎您。

Cheers 干杯

fg nu, fg nu,

We can not replicate the issue in our win platforms... can you please join us in the opened issue: https://github.com/ContinuumIO/bokeh/issues/682 to get more information about your setup and architecture? 我们无法在我们的胜利平台中复制这个问题...请您加入我们的开放式问题: https//github.com/ContinuumIO/bokeh/issues/682以获取有关您的设置和架构的更多信息?

We will very grateful if you can give us more info to replicate the issue and get a quick fix. 如果您能够提供更多信息来复制问题并快速解决问题,我们将非常感激。

Thanks. 谢谢。

Damian 达米安

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM