命名元组错误 [英] Namedtuple error

查看:42
本文介绍了命名元组错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 namedtuple 将 Python 对象序列化为 JSON.但我收到这个错误.谷歌没有帮助.

I am trying to serialize a Python object into JSON using namedtuple. But I get this error. Google does not help.

Traceback (most recent call last):
 File "cpu2.py", line 28, in <module>
 cpuInfo = collections.namedtuple('cpuStats',('cpu.usr', ('str(currentTime) + " " 
 +str(cpuStats[0]) + " host="+ thisClient')), ('cpu.nice', ('str(currentTime) + " " 
 +str(cpuStats[1]) + " host="+ thisClient')), ('cpu.sys',('str(currentTime) + " " 
 +str(cpuStats[2]) + " host="+ thisClient')), ('cpu.idle',('str(currentTime) + " " 
 +str(cpuStats[3]) + " host="+ thisClient')))
 TypeError: namedtuple() takes at most 4 arguments (5 given)

推荐答案

这里有一个 链接 到namedtuple 的文档.您没有正确初始化它.

Here is a link to the documentation for namedtuple. You aren't initializing it properly.

我猜你应该如何初始化它:

How I'm guessing you should initialize it:

cpuInfo = collections.namedtuple('cpuStats', ['usr', 'nice', 'sys', 'idle'])

# In this case, usr=str(currentTime) + " " +str(cpuStats[0]) + " host=" + thisClient
# You can figure the rest out...
info = cpuInfo(usr='fill',
               nice='this',
               sys='your',
               idle='self')

另外,您可能想阅读这个关于序列化的问题json 中的命名元组.

Also, you might want to read this question which talks about serializing namedtuples in json.

这篇关于命名元组错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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