如何将字典放入数组?

时间:2013-07-14 13:17:18

标签: python python-3.x

我想将History_Data,分开并放入字典中,然后将字典放入一维数组然后访问它们。但似乎有一些错误。我怎么解决这个问题?

这是我的代码

History_Data = ("2004/01/20,000006,29,28,13,33,34,32,43",
                "2004/01/18,000005,36,22,44,34,46,29,37",
                "2004/01/16,000004,02,13,34,44,06,40,14",
                "2004/01/14,000003,29,28,13,33,34,32,43",
                "2004/01/12,000002,32,15,14,29,39,20,43",
                "2004/01/10,000001,30,29,18,34,19,28,12")

Dataset = ()
for Line in History_Data:
    Item = {}
    Parts = Line.split(",") 
    Item['date'] = Parts[0]
    Item['serial'] = Parts[1]
    Item['numbers'] = Parts[2:len(Parts)]
    Dataset.append(Item)

for Element in Dataset:
    print(Element)

错误消息

Traceback (most recent call last):
  File ".\1.py", line 18, in <module>
    Dataset.append(Item)
AttributeError: 'tuple' object has no attribute 'append'

1 个答案:

答案 0 :(得分:6)

tuple是Python中的不可变类型,因此没有方法append。根据需要,使用列表Dataset = [],而不是元组Dataset = ()