2014-10-17 81 views
0

類型錯誤:int()函數的參數必須是字符串或數字,而不是 '元組' 使用CouchDB的Python

$ python test3.py 
828288 
Traceback (most recent call last): 
    File "test3.py", line 48, in <module> 
    test.pos = i[3], 
    File "/home/mic/.virtualenvs/test/lib/python2.7/site-packages/couchdb/mapping.py", line 105, in __set__ 
    value = self._to_json(value) 
    File "/home/mic/.virtualenvs/test/lib/python2.7/site-packages/couchdb/mapping.py", line 112, in _to_json 
    return self._to_python(value) 
TypeError: int() argument must be a string or a number, not 'tuple' 

與下面的腳本和CouchDB的 - python的(0.9):

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/ 


class Test(Document): 
    type = TextField()  # "Test" 
    name = TextField()  # "name" 
    sub_name = TextField() # "B01" 
    pos = IntegerField() # 828288 

    s_type = IntegerField() # 1 
    _id = IntegerField() # x_type = 7 
    chr = ListField(DictField(Mapping.build(
     letter=TextField(), # C 
     no=IntegerField(), # 5 
     ))) 


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] 

    test = Test() 

    test.type = i[0], 
    test.name = i[1], 
    test.sub_name = i[2], 
    test.pos = i[3], 
    test.s_type = i[4], 
    test._id = i[5], 
    test.chr.append(
     letter=i[6], 
     no=i[7] 
    ) 
    test.store(db) 

如何解決TypeError問題?

回答

1

你需要刪除代碼中的逗號,那麼你就沒有錯誤。

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/ 


class Test(Document): 
    type = TextField()  # "Test" 
    name = TextField()  # "name" 
    sub_name = TextField() # "B01" 
    pos = IntegerField() # 828288 

    s_type = IntegerField() # 1 
    _id = IntegerField() # x_type = 7 
    chr = ListField(DictField(Mapping.build(
     letter=TextField(), # C 
     no=IntegerField(), # 5 
     ))) 


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] 

    test = Test() 

    test.type = i[0] 
    test.name = i[1] 
    test.sub_name = i[2] 
    test.pos = i[3] 
    test.s_type = i[4] 
    test._id = i[5] 
    test.chr.append(
     letter=i[6], 
     no=i[7] 
    ) 
    test.store(db) 
相關問題