0

我想links根据 csv 文件中的数据为每个 couchdb 文档添加一个属性。links 属性的值是一个字典数组,其中包含链接文档的 couchdb_idlinkType

links当我运行脚本时,我收到一个链接错误(请参阅下面的错误信息),如果它不存在,我不确定如何创建 dict 键并添加链接数据,或者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'
4

2 回答 2

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
于 2012-10-05T23:28:25.657 回答
1

代替:

if doc['links'] in doc: 

和:

if 'links' in doc:
于 2012-10-05T23:24:41.783 回答