0

我有一个列表r,其中包含 12 个列表作为元素。每个列表都包含一些与其他列表相比相同的元素 ( i[0]to )。i[5]我只尝试插入唯一的文档,因此我应该在数据库中获得 5 个文档。如果文档已经存在,则追加i[6]i[7]chr.

import couchdb

# $ sudo systemctl start couchdb
# http://localhost:5984/_utils/

server = couchdb.Server()
try:
    db = server.create("test")
except couchdb.http.ResourceConflict:
    db = server["test"]

r = [["Test", "A", "B01", 828288,  1,    7, 'C', 5],
    ["Test", "A", "B01", 828288,  1,    7, 'T', 6],
    ["Test", "A", "B01", 171878,  3,    8, 'C', 5],
    ["Test", "A", "B01", 171878,  3,    8, 'T', 6],
    ["Test", "A", "B01", 871963,  3,    9, 'A', 5],
    ["Test", "A", "B01", 871963,  3,    9, 'G', 6],
    ["Test", "A", "B01", 1932523, 1,   10, 'T', 4],
    ["Test", "A", "B01", 1932523, 1,   10, 'A', 5],
    ["Test", "A", "B01", 1932523, 1,   10, 'X', 6],
    ["Test", "A", "B01", 667214,  1,   14, 'T', 4],
    ["Test", "A", "B01", 667214,  1,   14, 'G', 5],
    ["Test", "A", "B01", 667214,  1,   14, 'G', 6]]

_id = None
for i in r:
    _id = str(i[5])
    doc = {
        'type': i[0],
        'name': i[1],
        'sub_name': i[2],
        'pos': i[3],
        's_type': i[4],
        '_id': _id,
        'chr':[]
    }

    doc['chr'].append({
        "letter":i[6],
        "no":i[7]
    })

    a = db.save(doc)

UPDATE 中的前两个列表r包含相同的字段(i[0]to i[5])。两个列表的最终文件应该是这样的:

{
  "_id": "7",
  "_rev": "1-bc0b4e6f3aa855a486225f4a0dcd76c8",
  "sub_name": "B01",
  "name": "A",
  "pos": 828288,
  "s_type": 1,
  "chr": [
      {
          "letter": "C",
          "no": 5
      },
      {
          "letter": "T",
          "no": 6
      }
  ],
  "type": "Test"
}

如何更新文档并将字典附加到列表中?

4

1 回答 1

0

这是最有效的解决方案吗?

import couchdb

# $ sudo systemctl start couchdb
# http://localhost:5984/_utils/

server = couchdb.Server()
db = server.create("test")
# except couchdb.http.ResourceConflict:
#db = server["test"]

r = [["Test", "A", "B01", 828288,  1,    7, 'C', 5],
    ["Test", "A", "B01", 828288,  1,    7, 'T', 6],
    ["Test", "A", "B01", 171878,  3,    8, 'C', 5],
    ["Test", "A", "B01", 171878,  3,    8, 'T', 6],
    ["Test", "A", "B01", 871963,  3,    9, 'A', 5],
    ["Test", "A", "B01", 871963,  3,    9, 'G', 6],
    ["Test", "A", "B01", 1932523, 1,   10, 'T', 4],
    ["Test", "A", "B01", 1932523, 1,   10, 'A', 5],
    ["Test", "A", "B01", 1932523, 1,   10, 'X', 6],
    ["Test", "A", "B01", 667214,  1,   14, 'T', 4],
    ["Test", "A", "B01", 667214,  1,   14, 'G', 5],
    ["Test", "A", "B01", 667214,  1,   14, 'G', 6]]

# _id = None
for i in r:
    _id = str(i[5])
    doc = db.get(_id)
    if doc is None:
        doc = {
            'type': i[0],
            'name': i[1],
            'sub_name': i[2],
            'pos': i[3],
            's_type': i[4],
            '_id': _id,
            'chr':[]
        }

        doc['chr'].append({
            "letter":i[6],
            "no":i[7]
        })

    else:
        doc['chr'].append({
            "letter":i[6],
            "no":i[7]
        })

    db.save(doc)
于 2014-10-17T10:21:05.137 回答