访问嵌套值

时间:2017-05-16 16:22:49

标签: python

我希望将数据从一个表移动到另一个表,并在其间进行一些处理。数据是它的一个大型Json对象,所以我想要去除单个值正在选择数据。我将数据逐行传递给下面的函数。

def transformBadges(row):
    for inner_row in row:
        for element in inner_row :
            id = element['id']
            archived = element['archived']
            type = element['type']
            name = element['name']
            badge_id = str(uuid.uuid4())

            insertBadge(id, archived, type, name, badge_id)

row基本上包含我的目标列。但是我已经更新了我的选择以选择更多列。由于这个原因,我不得不将上面的代码改为:

def transformBadges(row):
    badges = row['badges']
    for inner_row in badges:
        for element in inner_row:
            id = element['id']
            archived = element['archived']
            type = element['type']
            name = element['name']
            badge_id = str(uuid.uuid4())

            insertBadge(id, archived, type, name, badge_id)

在我看来,它完全相同 - 只是现在数据存储在一个不同的变量中。但是,我收到了这个错误:

Traceback (most recent call last):
  File "etl/main.py", line 537, in <module>
    main()
  File "etl/main.py", line 99, in main
    transformBadges(row)
  File "etl/main.py", line 106, in transformBadges
    id = element['id']
TypeError: string indices must be integers

为什么第一个工作而不是第二个?

1 个答案:

答案 0 :(得分:2)

def transformBadges(row):
    badges = row['badges']
    for element in badges:
        id = element['id']
        archived = element['archived']
        type = element['type']
        name = element['name']
        badge_id = str(uuid.uuid4())

        insertBadge(id, archived, type, name, badge_id)

因为badges现在只有一个inner_row