我试图在使用 Python 3.3.0 的 64 位 Windows 机器上重现Bokeh 教程中最简单的示例。
这是完整的代码
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 用来写出修复的编码知之甚少这个。帮助表示赞赏。
编辑:
我尝试实施此处给出的建议,始终通过流写入器传递标准输出输出:
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
变数。
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'