情节:类型错误:无法将字典更新序列元素#0 转换为序列 [英] plotly: TypeError: cannot convert dictionary update sequence element #0 to a sequence

查看:102
本文介绍了情节:类型错误:无法将字典更新序列元素#0 转换为序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在处理的项目有问题,所以我留下了一个问题.
当前内容由传感器以表格形式绘制.但是前几天,当我在做某事时,我不知道如何解决它,所以我寻求帮助.

import plotly.plotly as py导入 plotly.figure_factory 作为 ff从 bluepy 导入 sensortag导入系统导入时间导入日期时间将 numpy 导入为 nppy.sign_in("smrlswja9963","94b4enXKYqyOu4iuPDGG")时间.sleep(1.0)tag = sensortag.SensorTag('24:71:89:CC:53:00')tag.IR温度.enable()标签.湿度.启用()stream1=py.Stream('liz9v2c0or')流1.open()我=0为真:现在 = datetime.datetime.now()dateTime = now.strftime("%Y-%m-%d %H:%M:%S")A = tag.IRtemperature.read()B = 标签.湿度.读取()诱惑=A[0]嗡嗡声=B[0]stream1.write({dateTime,tempt,humty})我+=1tag.waitForNotifications(3.0)table = go.Table(header=dict(values=["dateTime","tempt","humty"]),单元格=字典(值=[[日期时间],[诱惑],[humty]]),流 = dict(token='liz9v2c0or',))数据=[表]py.iplot(表,文件名=乳胶表")

执行以下代码后,出现如下错误.

回溯(最近一次调用最后一次): 中的文件/home/pi/do it.py",第 27 行stream1.write({dateTime,tempt,humty})文件/usr/local/lib/python2.7/dist-packages/plotly/plotly/plotly.py",第632行,写入stream_object.update(trace)类型错误:无法将字典更新序列元素 #0 转换为序列

在这种情况下,我使用 polty 绘制图形的代码在从流接收数据时没有问题,但是当我绘制表格图时.

我如何在这里绘制图表?我真的很好奇.请告诉我.

解决方案

此异常意味着您正在尝试从一个可迭代对象构造一个 dict,并且该可迭代对象的第一个元素不是一个序列.正如文档所述,您可以通过两种方式构建字典:>

  • 来自映射,或
  • 来自可迭代的键值对

因此,如果您尝试从一组数字构建它:

<预><代码>>>>dict({1, 2, 3})类型错误:无法将字典更新序列元素 #0 转换为序列

…它试图将第一个元素用作键值对——即一个由 2 个值组成的序列——但是没有办法将数字 1 解释为键值对,所以它会引发一个 TypeError.

<小时>

与此同时,我对 Plotly 流媒体一无所知,但是这个页面上有什么,但是这段代码是明显错了:

stream1.write({dateTime,tempt,humty})

我无法想象您为什么要流式传输集.

此外,这些示例都有一个 dict 或一个 JSON 编码的 dict 字符串.

因此,显然,该 API 期望您将 dict 或您可以提供给 dict 构造函数的内容传递给它.但是你传递了一个集合.因此,它将该集合提供给 dict 构造函数,并获得此异常.

因为我不知道你在这里真正想要做什么,所以我不知道你应该在这里发送什么指令.但你绝对应该传递一个字典.

<小时>

此外,即使您修复了这个问题,基于 sign_in 调用,看起来您正在使用 Plotly Cloud.但是,正如同一页所说:

<块引用>

Plotly Cloud 不再支持流式传输.

因此,如果您尝试将流式传输与 Plotly Cloud 一起使用,那么即使您修复了代码以使其有意义,它也可能仍然会失败,只是来自 Plotly 的错误而不是 TypeError 关于传递废话.

I have a problem with the project I'm currently working on, so I'm leaving a question.
The current contents are drawn in table format by sensor. But a few days ago, when I was working on something, I didn't know how to fix it, so I asked for help.

import plotly.plotly as py
import plotly.figure_factory as ff
from bluepy import sensortag
import sys
import time
import datetime
import numpy as np

py.sign_in("smrlswja9963","94b4enXKYqyOu4iuPDGG")

time.sleep(1.0)

tag = sensortag.SensorTag('24:71:89:CC:53:00')
tag.IRtemperature.enable()
tag.humidity.enable()

stream1=py.Stream('liz9v2c0or')
stream1.open()

i=0     
while True:
    now = datetime.datetime.now()
    dateTime = now.strftime("%Y-%m-%d %H:%M:%S")

    A = tag.IRtemperature.read()
    B = tag.humidity.read()
    tempt=A[0]
    humty=B[0]

    stream1.write({dateTime,tempt,humty})

    i+=1

    tag.waitForNotifications(3.0)     

table = go.Table(
    header=dict(values=["dateTime","tempt","humty"]),
    cells=dict(values=[[dateTime],[tempt],[humty]]),
    stream = dict(token='liz9v2c0or',))

data=[table]
py.iplot(table, filename="latex table")

After executing the following code, the following error occurred.

Traceback (most recent call last):
  File "/home/pi/do it.py", line 27, in <module>
    stream1.write({dateTime,tempt,humty})
  File "/usr/local/lib/python2.7/dist-packages/plotly/plotly/plotly.py", line 632, in write
    stream_object.update(trace)
TypeError: cannot convert dictionary update sequence element #0 to a sequence

In this case, the code that I draw the graph using plolty does not have a problem when receiving data from stream, but when I draw table plot.

How can I graph here? I'm really curious. Please let me know.

解决方案

This exception means that you're trying to construct a dict from an iterable, and that iterable's first element is not a sequence. As the docs explain, you can construct a dict two ways:

  • From a mapping, or
  • From an iterable of key-value pairs

So, if you try to construct it from, say, a set of numbers:

>>> dict({1, 2, 3})
TypeError: cannot convert dictionary update sequence element #0 to a sequence

… it's trying to use the first element as a key-value pair—that is, a sequence of 2 values—but there's no way to interpret the number 1 as a key-value pair, so it raises a TypeError.


Meanwhile, I know absolutely nothing about Plotly streaming but what's on this page, but this code is clearly wrong:

stream1.write({dateTime,tempt,humty})

I can't imagine why you'd want to stream a set.

Plus, the examples all have either a dict, or a string that's a JSON-encoding of a dict.

So, obviously, that API is expecting you to pass it either a dict or something you can feed to the dict constructor. But you're passing it a set. So, it feeds that set to the dict constructor, and gets this exception.

Since I have no idea what you're actually trying to do here, I have no idea what dict you should be sending here. But you definitely should be passing a dict.


Also, even if you fix this, based on the sign_in call, it looks like you're using Plotly Cloud. But, as the same page says:

Streaming is no longer supported in Plotly Cloud.

So, if you're trying to use streaming with Plotly Cloud, then, even if you fix your code to make sense, it's probably still going to fail, just with an error from Plotly rather than a TypeError about passing nonsense.

这篇关于情节:类型错误:无法将字典更新序列元素#0 转换为序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆