Bokeh' utf8'编解码器不能解码字节0xe9:意外的数据结束

时间:2017-11-21 05:11:45

标签: python pandas encoding bokeh

我使用Bokeh绘制一个pandas Dataframe。以下是代码:

map_options = GMapOptions(lat=19.075984, lng=72.877656, map_type="roadmap", zoom=11)
plot = GMapPlot(x_range=DataRange1d(), y_range=DataRange1d(), map_options=map_options)


plot.api_key = "xxxxx"
source = ColumnDataSource(
    data=dict(
        lat=[float(i) for i in data.lat],
        lon=[float(i) for i in data.lon],
        size=[int(i)/1000 for i in data['count']],
        ID = [i for i in data.merchant_id],
        Merchant = [str(i) for i in data.merchant_name],
        count = [float(i) for i in data['count']]
    )
)
hover = HoverTool(tooltips=[
    ("(x,y)", "($lat, $lon)"),
    ("ID", "$ID"),
    ("Name", "@Merchant"),
    ("count","$count")
])


# hover.renderers.append(circle_glyph)
plot.tools.append(hover)
circle = Circle(x="lon", y="lat", size='size', fill_color="blue", fill_alpha=0.8, line_color=None)
plot.add_glyph(source, circle)

# plot.add_layout(labels)
plot.add_tools(PanTool(), WheelZoomTool(), BoxSelectTool())
output_file("gmap_plot.html")
show(plot)

在Hovertool中使用" Name"字段引发以下错误:

  

UnicodeDecodeError:' utf8'编解码器不能解码位置6中的字节0xe9:   意外的数据结束

同时评论"名称"字段仍然给我错误,但有一个输出图。

以下是我使用的数据框:

    lat lon merchant_id count   merchant_name
0   18.539971   73.893963   757 777 Portobello
1   18.565766   73.910980   745 10193   The Wok Box
2   18.815427   76.775143   1058    2354    Burrito Factory
3   18.914633   72.817916   87  1985    Flamboyante
4   18.915794   72.824370   94  1116    Butterfly Pond
5   18.916473   72.826868   145 1010    Leo's Boulangerie
6   18.918923   72.828325   115 517 Brijwasi Sweets
7   18.928063   72.832888   973 613 Pandora's Box
8   18.928562   72.832353   101 64  La Folie Patisserie
9   18.929516   72.831860   961 6673    Burma Burma

据我所知,商家名称中包含导致错误的字符,但我尝试使用“utf-8'”' ascii&#39对列进行编码;等等但是我收到以下错误:

data['merchant_name'] = data['merchant_name'].str.encode('utf-8')
  

UnicodeDecodeError:' ascii'编解码器不能解码位置6中的字节0xe9:序数不在范围内(128)

关于如何进行的任何想法?

2 个答案:

答案 0 :(得分:3)

字节0xe9不是纯粹的ascii,因为它是233(在十进制系统中)而ascii只有127个符号。在UTF-8中,它是一个特殊字节,它引入了接下来两个字节的字符。因此字符串可能是另一种编码。例如,在latin1和latin2中,字节0xe9表示字母é。

请记住,首先你必须解码字符串。你试过编码str,(普通字符串)类型没有意义。因此,Python尝试了他的默认decode('ascii'),并且您在UnicodeDecodeError方法上获得了encode

我没有设法复制错误,也没有在您提供的数据中看到任何特殊字符(特别是我没有看到0xe9字节)。所以我只能猜测。我会尝试这样的事情:

data['merchant_name'] = data['merchant_name'].str.decode('latin1').encode('utf-8')

最后但并非最不重要的请当您发布代码时,请发布包含所有导入和所有内容的完整代码。我从未使用过Bokeh,现在,当我试图复制你的错误时,重建它们很费时间。 (但无论如何 - 最后我设法导入所有内容,但我没有收到你的错误。)

答案 1 :(得分:0)

我解决了这样的问题

tsne_df['words'] = list(w2v_model.wv.vocab.keys())[:5000]

tsne_df['words'] = tsne_df['words'].str.decode('latin1').str.encode('utf-8')

plot_tfidf.scatter(x='x', y='y', source=tsne_df)
hover = plot_tfidf.select(dict(type=HoverTool))
hover.tooltips={"word": "@words"}
show(plot_tfidf)