Python:在Windows上编码有问题(Bokeh绘图库)
我试图在一台64位Windows机器上,用Python 3.3.0重现最简单的示例 下面是完整的代码Python:在Windows上编码有问题(Bokeh绘图库),python,python-3.x,character-encoding,bokeh,Python,Python 3.x,Character Encoding,Bokeh,我试图在一台64位Windows机器上,用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 f
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>
但是一些接口似乎已经更改,并且没有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'
回溯(最近一次呼叫最后一次):
文件“F:\programming\python\python64\python33\lib\site packages\IPython\core\interactiveshell.py”,第2732行,运行代码
exec(代码对象、self.user\u全局、self.user\n)
文件“”,第1行,在
如果sys.stdout.encoding!='UTF-8':
文件“F:\programming\python\python64\python33\lib\codecs.py”,第387行,位于\uu getattr中__
返回getattr(self.stream,name)
AttributeError:“\u io.FileIO”对象没有“encoding”属性
我已打开一个问题来跟踪此问题:
正如您与eryksun讨论过的,它似乎很容易修复
我会在这里向你通报最新情况,但如果你想参与这个问题,我们非常欢迎你
干杯
我们不能在我们的win平台中复制此问题。。。您能否加入我们的公开发行:获取有关您的设置和体系结构的更多信息
如果您能给我们提供更多信息以复制此问题并获得快速解决方案,我们将不胜感激
谢谢
Damian您的问题与
sys.stdout
无关,但是如果您想在Python 3中替换它,请使用sys.stdout=io.TextIOWrapper(sys.stdout.detach(),'utf-8','strict')
。不设置文件的编码。它使用来自的默认值。我害怕使用它,你需要在bokeh目录中搜索monkey patchbokeh.plotting
或locale
(\u bootlocale
)@eryksun-Hmm,我在bokeh目录中搜索了GetPreferedEncoding
,但它似乎没有被使用。您能告诉我哪些代码行可以显式设置输出编码吗?open(filename,“w”)
如果您不提供显式的编码,例如open(filename,“w”,encoding=“utf-8”),则隐式使用getpreferedencoding
@eryksun所以只需添加encoding='utf-8'
选项?我在Windows中用cp1252
尝试了这一点,但没有出现错误。出于某种原因,fg nu的输出有一个无法在cp1252
中编码的字符。但实际上,仅限于一个包含250个字符的8位代码页是上个世纪的事。输出文件需要一个选项来设置编码和UTF-8的跨平台默认值。我们目前正在讨论这个问题。可选编码可能会有问题,因为无法使用所有可用的编码选项对Bokeh内容进行编码。但是我们正在评估您提到的默认UTF-8的可能性。由于您已经在github上解决了这个问题,我将此标记为可接受的答案。不过,与wontfix的玩乐很有趣。
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'