0

如何_id在 couchdb-python (0.9) 中定义 own,因为当我尝试时,'_id': i[5]我收到以下错误消息?

$ python test3.py
828288
Traceback (most recent call last):
  File "test3.py", line 42, in <module>
    db.save(doc)
  File "/home/mictadlo/.virtualenvs/unisnp/lib/python2.7/site-packages/couchdb/client.py", line 415, in save
    func = _doc_resource(self.resource, doc['_id']).put_json
  File "/home/mictadlo/.virtualenvs/unisnp/lib/python2.7/site-packages/couchdb/client.py", line 954, in _doc_resource
    if doc_id[:1] == '_':
TypeError: 'int' object has no attribute '__getitem__'

以下是导致上述错误的脚本:

from couchdb.mapping import Document, TextField, IntegerField, Mapping
from couchdb.mapping import DictField, ViewField, BooleanField, ListField
from couchdb import Server

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

server = Server()
db = server.create("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]]


for i in r:
    print i[3]

    doc = {
        'type': i[0],
        'name': i[1],
        'sub_name': i[2],
        'pos': i[3],
        's_type': i[4],
        '_id': i[5],
        'chr':[]
    }
    doc['chr'].append({
        "letter":i[6],
        "no":i[7]
    })

    db.save(doc)
4

1 回答 1

1

它期望_id是一个字符串,并且您正在传递一种 int 类型。错误是由这一行引起的:

if doc_id[:1] == '_':

因为脚本试图切片一个 int 对象。

所以改成字符串类型:

...
...
'_id': str(i[5]),
...
于 2014-10-17T04:32:50.860 回答