Mongo数据库操作:
#!/user/bin/env python3
# -*- coding: utf-8 -*-
from pymongo import MongoClient
COLSIZ = 10
FIELDS = ('login', 'userid', 'projid')
cxn = MongoClient(host='192.168.1.30',port=27017)
db = cxn['test']
users = db['users']
def tformat(s): return str(s).title().ljust(COLSIZ)
NAMES = (
('arron', 8312), ('angela', 7603), ('dave', 7306),
('davina', 7902), ('elliot', 7911), ('ernie', 7410),
('jess', 7912), ('jim', 7512), ('larry', 7311),
('leslie', 7808), ('melissa', 8602), ('pat', 7711),
('serena', 7003), ('stan', 7607), ('faye', 6812),
('amy', 7209), ('mona', 7404), ('jennifer', 7608),
)
def randName():
pick = set(NAMES) # 内置函数set()创建了一个无序不重复元素集,可进行关系测试,删除重复数据,还可以计算交集、差集】并集等
while pick:
yield pick.pop()
users.insert_many(
dict(login=who,userid=uid,projid=1)
for who,uid in randName())
for user in users.find():
print(''.join(map(tformat, (user[k] for k in FIELDS))))
输出结果:
Davina 7902 1
Mona 7404 1
Angela 7603 1
Arron 8312 1
Larry 7311 1
Stan 7607 1
Serena 7003 1
Jim 7512 1
Elliot 7911 1
Faye 6812 1
Pat 7711 1
Leslie 7808 1
Ernie 7410 1
Amy 7209 1
Jennifer 7608 1
Dave 7306 1
Jess 7912 1
Melissa 8602 1
Serena 7003 1
Ernie 7410 1
Larry 7311 1
Stan 7607 1
Pat 7711 1
Leslie 7808 1
Melissa 8602 1
Amy 7209 1
Jennifer 7608 1
Faye 6812 1
Elliot 7911 1
Mona 7404 1
Jess 7912 1
Davina 7902 1
Angela 7603 1
Dave 7306 1
Arron 8312 1
Jim 7512 1
拆分输出结果:
x = users.find()
print(x)
输出:
<pymongo.cursor.Cursor object at 0x058A02D0>
返回结果是Cursor类型,相当于一个生成器,我们需要遍历取到所有的结果,每一个结果都是字典类型
修改为:
for xx in x:
print(xx)
输出:
{'_id': ObjectId('5d01c4b4bce2b3d6bdb94ec0'), 'login': 'angela', 'userid': 7603, 'projid': 1}
{'_id': ObjectId('5d01c4b4bce2b3d6bdb94ec1'), 'login': 'dave', 'userid': 7306, 'projid': 1}
{'_id': ObjectId('5d01c4b4bce2b3d6bdb94ec2'), 'login': 'jim', 'userid': 7512, 'projid': 1}
{'_id': ObjectId('5d01c4b4bce2b3d6bdb94ec3'), 'login': 'davina', 'userid': 7902, 'projid': 1}
{'_id': ObjectId('5d01c4b4bce2b3d6bdb94ec4'), 'login': 'elliot', 'userid': 7911, 'projid': 1}
{'_id': ObjectId('5d01c4b4bce2b3d6bdb94ec5'), 'login': 'amy', 'userid': 7209, 'projid': 1}
{'_id': ObjectId('5d01c4b4bce2b3d6bdb94ec6'), 'login': 'stan', 'userid': 7607, 'projid': 1}
{'_id': ObjectId('5d01c4b4bce2b3d6bdb94ec7'), 'login': 'serena', 'userid': 7003, 'projid': 1}
{'_id': ObjectId('5d01c4b4bce2b3d6bdb94ec8'), 'login': 'ernie', 'userid': 7410, 'projid': 1}
{'_id': ObjectId('5d01c4b4bce2b3d6bdb94ec9'), 'login': 'pat', 'userid': 7711, 'projid': 1}
{'_id': ObjectId('5d01c4b4bce2b3d6bdb94eca'), 'login': 'mona', 'userid': 7404, 'projid': 1}
{'_id': ObjectId('5d01c4b4bce2b3d6bdb94ecb'), 'login': 'leslie', 'userid': 7808, 'projid': 1}
{'_id': ObjectId('5d01c4b4bce2b3d6bdb94ecc'), 'login': 'melissa', 'userid': 8602, 'projid': 1}
{'_id': ObjectId('5d01c4b4bce2b3d6bdb94ecd'), 'login': 'jennifer', 'userid': 7608, 'projid': 1}
{'_id': ObjectId('5d01c4b4bce2b3d6bdb94ece'), 'login': 'jess', 'userid': 7912, 'projid': 1}
{'_id': ObjectId('5d01c4b4bce2b3d6bdb94ecf'), 'login': 'larry', 'userid': 7311, 'projid': 1}
{'_id': ObjectId('5d01c4b4bce2b3d6bdb94ed0'), 'login': 'faye', 'userid': 6812, 'projid': 1}
{'_id': ObjectId('5d01c4b4bce2b3d6bdb94ed1'), 'login': 'arron', 'userid': 8312, 'projid': 1}
继续拆分输出结果:
FIELDS = ('login', 'userid', 'projid')
COLSIZ =10
def cformat(s): return s.upper().ljust(COLSIZ)
a = map(cformat,FIELDS)
print('\n%s' % ''.join(a))
LOGIN USERID PROJID
尝试直接输出a的值:
print (a)
<map object at 0x04DD0630>
修改为list类型输出:
print (list(a))
['LOGIN ', 'USERID ', 'PROJID ']
dict函数报错:
a = [dict(login=who, userid=uid, projid=3) for who, uid in randName()]
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "<input>", line 1, in <listcomp>
TypeError: 'dict' object is not callable
原因是之前定义过dict,有这么一条赋值语句
dict = {'a':1,'b':2}
导致无法运行python内置函数dict,运行以下语句
del(dict)
不再报错
使用join函数时
print( "_".join("a","b","c"))
报错:
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: join() takes exactly one argument (3 given)
应该为
print( "_".join(("a","b","c")))
a_b_c