在python中添加一个链接文档_ids数组到couchdb文档

时间:2012-10-05 23:04:39

标签: python dictionary couchdb relationship

我想根据csv文件中的数据为每个couchdb文档添加links属性。 links属性的值是包含链接文档的couchdb _idlinkType

的dicts数组

当我运行脚本时,我收到链接错误(请参阅下面的错误信息) 我不知道如果dict键links不存在并且添加链接数据,或者如果它存在则附加到links数组,我不知道如何创建它。

带有链接的文档示例如下所示:

{
    _id: p_3,
    name: 'Smurfette'
    links: [
                {to_id: p_2, linkType: 'knows'},
                {to_id: o_56, linkType: 'follows'}
           ]
}

用于处理csv文件的python脚本:

#!/usr/bin/python
# coding: utf-8

# Version 1
# 
# csv fields: ID,fromType,fromID,toType,toID,LinkType,Directional


import csv, sys, couchdb


def csv2couchLinks(database, csvfile):

    # CouchDB Database Connection etc
    server = couchdb.Server()
    #assumes that couchdb runs on http://localhost:5984
    db = server[database]
    #assumes that db is already created

    # CSV file
    data = csv.reader(open(csvfile, "rb")) # Read in the CSV file rb=read/binary
    csv_links= csv.DictReader(open(csvfile, "rb"))


    def makeLink(from_id, to_id, linkType):
        # get doc from db
        doc = db[from_id]

        # construct link object
        link = {'to_id':to_id, 'linkType':linkType}

        # add link reference to array at key 'links'
        if doc['links'] in doc:
            doc['links'].append(link)
        else:
            doc['links'] = [link]

        # update the record in the database
        db[doc.id] = doc


    # read each row in csv file
    for row in csv_links:

        # get entityTypes as lowercase and entityIDs
        fromType = row['fromType'].lower()
        fromID   = row['fromID']
        toType   = row['toType'].lower()
        toID     = row['toID']

        linkType = row['LinkType']

        # concatenate 'entity type' and 'id' to make couch '_id'
        fromIDcouch = fromType[0]+'_'+fromID #eg 'p_2' <= person 2
        toIDcouch = toType[0]+'_'+toID

        makeLink(fromIDcouch, toIDcouch, linkType)
        makeLink(toIDcouch, fromIDcouch, linkType)


# Run csv2couchLinks() if this is not an imported module
if __name__ == '__main__':
    DATABASE = sys.argv[1]
    CSVFILE = sys.argv[2]
    csv2couchLinks(DATABASE,CSVFILE)   

错误信息:

$ python LINKS_csv2couchdb_v1.py "qmhonour" "./tablesAsCsv/links.csv"
Traceback (most recent call last):
  File "LINKS_csv2couchdb_v1.py", line 65, in <module>
    csv2couchLinks(DATABASE,CSVFILE)   
  File "LINKS_csv2couchdb_v1.py", line 57, in csv2couchLinks
    makeLink(fromIDcouch, toIDcouch, linkType)
  File "LINKS_csv2couchdb_v1.py", line 33, in makeLink
    if doc['links'] in doc:
KeyError: 'links'

2 个答案:

答案 0 :(得分:2)

另一个选择是将if块压缩为:

doc.setdefault('links', []).append(link)

字典的setdefault方法检查字典中是否存在links,如果不存在,则会创建一个键并将值设为空列表(默认值)。然后它会将link附加到该列表中。如果links确实存在,则只需将link附加到列表中。

def makeLink(from_id, to_id, linkType):
    # get doc from db
    doc = db[from_id]

    # construct link object
    link = {'to_id':to_id, 'linkType':linkType}

    # add link reference to array at key 'links'
    doc.setdefault('links', []).append(link)

    # update the record in the database
    db[doc.id] = doc

答案 1 :(得分:1)

替换:

if doc['links'] in doc: 

使用:

if 'links' in doc: