plotly 给出错误消息"TypeError: __init__() got an unexpected keyword argument 'encoding'" - Python



我昨天才遇到Plotly,我正在尝试找到一种以很好的方式生成表格的方法,类似于在Matplotlib中生成图表。

最初,我尝试使用自己的数据,并在标题中不断收到错误消息。所以我从plotly网站上复制并粘贴了确切的代码,但仍然收到此错误。以前有人遇到过这种情况吗?有没有人有解决方案。我觉得这不是我的代码的简单问题。

import plotly.plotly as py
import plotly.graph_objs as go
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_usa_states.csv')
trace = go.Table(
    header=dict(values=list(df.columns),
                fill = dict(color='#C2D4FF'),
                align = ['left'] * 5),
    cells=dict(values=[df.Rank, df.State, df.Postal, df.Population],
               fill = dict(color='#F5F8FF'),
               align = ['left'] * 5))
data = [trace]
py.iplot(data, filename = 'pandas_table')

这是我使用的代码,导致以下错误:

TypeError: __init__() got an unexpected keyword argument 'encoding'

如果有人有情节的替代方案,我可以制作出漂亮的表格,这也将非常有帮助。

非常感谢

取决于您的环境。如果您使用的是笔记本,qgrid 是处理熊猫数据帧的非常好的工具,但我不知道它是否适用于其他环境。

关于代码,它通过更改对我有用(再次在笔记本环境中(

import plotly.plotly as py

import plotly.offline as py
py.init_notebook_mode(connected=False)

我倾向于使用离线情节,这是情节 3 的标准。

它看起来像一个错误。如果您赶时间并且迫不及待地等待修复,您可以这样做:

打开/usr/lib/python3/dist-packages/simplejson/__init__.py并在dumps方法中编辑"cls"变量,如下所示: cls = JSONEncoder

所以它看起来像这样:

    if cls is None:
        cls = JSONEncoder
    cls = JSONEncoder
    return cls(
        skipkeys=skipkeys, ensure_ascii=ensure_ascii,
        check_circular=check_circular, allow_nan=allow_nan, indent=indent,
        separators=separators, encoding=encoding, default=default,
        use_decimal=use_decimal,
        namedtuple_as_object=namedtuple_as_object,
        tuple_as_array=tuple_as_array,
        iterable_as_array=iterable_as_array,
        bigint_as_string=bigint_as_string,
        sort_keys=sort_keys,
        item_sort_key=item_sort_key,
        for_json=for_json,
        ignore_nan=ignore_nan,
        int_as_string_bitcount=int_as_string_bitcount,
        **kw).encode(obj)

基本上强制它使用默认的JSONEncoder而不是Plotly编码器。还要确保更改不会中断使用 JSON 的任何其他代码。它对我有用,但肯定有更好的解决方案。

相关内容

最新更新