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

风流意气都作罢 提交于 2019-12-13 02:26:53

问题


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

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. Help appreciated.

Edit:

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

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'

回答1:


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

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.

Cheers




回答2:


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?

We will very grateful if you can give us more info to replicate the issue and get a quick fix.

Thanks.

Damian



来源:https://stackoverflow.com/questions/24075840/python-trouble-with-encoding-on-windows-bokeh-plotting-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!