Python 属性错误:';dict';对象没有属性';表u id';当属性存在时
嗨,我在一系列字典上进行了一次艰难的迭代,无法找出失败的地方 以下是数据:Python 属性错误:';dict';对象没有属性';表u id';当属性存在时,python,Python,嗨,我在一系列字典上进行了一次艰难的迭代,无法找出失败的地方 以下是数据: total = [{'table_id': 'IA_AUTO_2020-11-25', 'created': datetime.datetime(2020, 11, 25, 5, 36, 1, 281000, tzinfo=datetime.timezone.utc)}, {'table_id': 'IA_AUTO_2020-12-07', 'created': datetime.datetime(2020, 1
total = [{'table_id': 'IA_AUTO_2020-11-25', 'created': datetime.datetime(2020, 11, 25, 5, 36, 1, 281000, tzinfo=datetime.timezone.utc)},
{'table_id': 'IA_AUTO_2020-12-07', 'created': datetime.datetime(2020, 12, 7, 5, 55, 4, 142000, tzinfo=datetime.timezone.utc)},
{'table_id': 'IA_AUTO_2020-12-09', 'created': datetime.datetime(2020, 12, 9, 5, 52, 55, 48000, tzinfo=datetime.timezone.utc)}]
这是迭代代码:
n=[]
d=[]
for t in total:
n.append(t.table_id)
d.append(t.created)
total.append(t.table_id)
total.append(t.created)
print(n)
但我得到了一个错误:
` for t in total:
print(t)
> n.append(t.table_id)
for t in total('trending_CL'):
TypeError: 'list' object is not callable`
一个重要注意事项:我不能更改循环结构,但rater修改数据结构使其正常工作
我知道问题一定很简单,但确实找不到它您可以使用
my_dict['my_key']
访问字典的键
以下代码应该可以工作:
n=[]
d=[]
对于客户端中的t,列出表格(“趋势表”):
n、 追加(t['table_id'])
d、 追加(t['created'])
总计.追加(t['table_id'])
总计.追加(t['created'])
您不是调用dict的属性(对于属性,使用obj.attr
是正确的),而是调用dict的键,实际上dict的项是按键命名的,因此要访问它们,您必须执行dict[“KEY”]
并修改它们dict[“KEY”]=“value”
因此,您的代码应该是:
n=[]
d=[]
for t in client.list_tables('trending_CL'):
n.append(t['table_id'])
d.append(t['created'])
total.append(t['table_id'])
total.append(t['created'])
字典用来访问它们的值-。
t.table\u id
应该是t['table\u id']
请您的问题包含一个。不清楚什么是client.list\u tables
here@Tomerikoo我编辑了这个问题,因为我试图模拟数据,这会引发一个错误:`E NameError:name'table_id'未定义`@JuanPenaloza,这在上述代码中是不可能的……您是否正确地在table_id
之前和之后转录了“
?在您的代码中,您正在查找名为table\u id
的变量,而不是字符串'table\u id'
@Tomerikoo当我在一个新的干净文件中执行这组代码时,我总共得到了t的('trending\u CL'):TypeError:'list'对象是不可调用的
您说“不可能”是什么意思?@JuanPenaloza这不是你之前报告的错误。从上面的代码来看,前面的代码是不可能的。您提到的第二个错误很难说,因为我们不知道什么是list\u tables
。在问题中发表一篇文章