我想在其中进行一些处理的同时将数据从一个表移动到另一个表。数据在源头上是一个很大的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
为什么要先做而不要做第二件事?
最佳答案
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
关于python - 访问嵌套值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44006756/