【问题标题】:populate an intersection table with data from unrelated tables使用来自不相关表的数据填充交集表
【发布时间】:2017-09-26 20:31:58
【问题描述】:

我正在从网络上抓取的 json 数据构建一个关系数据库(多对多)。我有两个主要数据库,一个有 909 行,另一个有 13 行。我需要将大表的 id 与交集表中的小表的 id 匹配,对应于它们在 json 文件中的链接方式。我发现的问题是没有将这些表关联在一起,但我需要填充交集表。除了需要几天时间的手工操作之外,我没有尝试或想到会填充它。下面是一个 json 文件的小样本。大表有课程信息,小表使用fulfills键。

[
{"number": "CHIN 242", "subject": "Chinese", "title": "Chinese Cinema and Chinese Modernity", "description": "From the fall of the Clestial Empire to the rise of China's economy today, Chinese cinema has witnessed many social changes in the modern era. This course will focus on the interaction between Chinese cinema and the process of modernization.", "fulfills": ["Human Expression\u2014Primary Texts", "Intercultural"]},

{"number": "CLAS 240", "subject": "Classics", "title": "Classical Mythology", "description": "A survey of the major myths and legends of ancient Greece and Rome.", "fulfills": ["Human Expression\u2014Primary Texts", "Quantitative"]},

{"number": "CLAS 250", "subject": "Classics", "title": "The World of Ancient Greece", "description": "A historical survey of ancient Greek culture from the Trojan War to the rise of Rome.", "fulfills": ["Human Expression\u2014Primary Texts", "Religion"]},

{"number": "CLAS 255", "subject": "Classics", "title": "Ancient Roman Culture", "description": "This course explores various cultural institutions and practices of the ancient Romans.", "fulfills": ["Human Expression\u2014Primary Texts", "Human Behavior"]},

{"number": "CLAS 265", "subject": "Classics", "title": "Greece and Rome on Film", "description": "This course explores the ways in which various events and episodes from Greek and Roman myth and history have been adapted for modern film and television.", "fulfills": ["Human Expression\u2014Primary Texts"]},

{"number": "CLAS 270", "subject": "Classics", "title": "Archaeology of Ancient Greece", "description": "An in-depth study of the archaeology of ancient Greece, with a focus on the high points of Greek civilization and material culture.", "fulfills": ["Historical", "Human Expression\u2014Primary Texts"]},

{"number": "CLAS 275", "subject": "Classics", "title": "Archaeology of Ancient Rome", "description": "This course explores the archaeology of ancient Rome from its early beginnings to its rapid growth into one of the world's largest empires.", "fulfills": ["Historical", "Human Expression\u2014Primary Texts"]},

{"number": "CLAS 300", "subject": "Classics", "title": "Classics and Culture", "description": "Using texts in translation, this course explores select aspects or themes from the cultures of ancient Greece and Rome.", "fulfills": ["Human Expression\u2014Primary Texts"]},
]

【问题讨论】:

  • 为什么这被视为如此消极?我实际上是在一个岛上,希望有更多经验的人能帮助我解决一个我花了将近一个半星期的时间试图弄清楚的问题。

标签: python postgresql psycopg2


【解决方案1】:

我找到了解决方案。要填充交集表,请在 id 上运行查询并在小表上执行查询,以及 id 和类似数字之类的简单内容,然后为每个查询运行 myDict = dict(map(reversed,cur.fetchall()))。从那里,使用 for 循环来迭代 json 数据,然后使用嵌套的 for 循环来迭代fulfills 和cur.execute(insert into ...) 中的列表项。它看起来像这样:

cur.execute('select id, fulfills from reqs;')
dict1 = dict(map(reversed,cur.fetchall()))
cur.execute('select id, number from course;')
dict2 = dict(map(reversed,cur.fetchall()))
for x in geneds:
    for y in x:
        cur.execute('insert into table(course, req) values(%s, %s);', (dict2[x['number']], dict1[y]))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 2016-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多