python mongoDB相關代碼
根據id查詢(pymongo 2.4.1-3.1.1)
from bson.objectid import ObjectId
for item in dbm.neo_nodes.find({"_id": ObjectId(obj_id_to_find)})]
http://stackoverflow.com/questions/16073865/search-by-objectid-in-mongodb-with-pymongo
http://www.jb51.net/article/66425.htm(過時的方法)
一些api:
mydb = con.mydb # new a database
mydb.add_user('test', 'test') # add a user
mydb.authenticate('test', 'test') # check auth
muser = mydb.user # new a table
muser.save({'id':1, 'name':'test'}) # add a record
muser.insert({'id':2, 'name':'hello'}) # add a record
muser.find_one() # find a record
muser.find_one({'id':2}) # find a record by query
muser.create_index('id')
muser.find().sort('id', pymongo.ASCENDING) # DESCENDING
# muser.drop() delete table
muser.find({'id':1}).count() # get records number
muser.find({'id':1}).limit(3).skip(2) # start index is 2 limit 3 records
muser.remove({'id':1}) # delet records where id = 1
muser.update({'id':2}, {'$set':{'name':'haha'}}) # update one recor
http://www.cnblogs.com/DxSoft/archive/2010/10/21/1857371.html
如果出現:AttributeError: ‘module’ object has no attribute ‘Connection’
這個錯誤,是因爲版本pymongo.Connection()在新版本中,這個方法已經不存在了,應該這樣用:
from pymongo import MongoClient
client=MongoClient()#鏈接默認的host和port
client=MongoClient('localhost', 27017)
client.db.table.find()
枚舉find的結果:
cursor = list(table.find().limit(10).skip(10*(page-1)))
for item in cursor:
print item
部分命令:
連接數據庫
>>> db = conn.ChatRoom
或
>>> db = conn['ChatRoom']
連接聚集
>>> account = db.Account
或
>>> account = db["Account"]
查看全部聚集名稱
>>> db.collection_names()
查看聚集的一條記錄
>>> db.Account.find_one()
>>> db.Account.find_one({"UserName":"keyword"})
查看聚集的字段
>>> db.Account.find_one({},{"UserName":1,"Email":1})
{u'UserName': u'libing', u'_id': ObjectId('4ded95c3b7780a774a099b7c'), u'Email': u'[email protected]'}
>>> db.Account.find_one({},{"UserName":1,"Email":1,"_id":0})
{u'UserName': u'libing', u'Email': u'[email protected]'}
查看聚集的多條記錄
>>> for item in db.Account.find():
item
>>> for item in db.Account.find({"UserName":"libing"}):
item["UserName"]
查看聚集的記錄統計
>>> db.Account.find().count()
>>> db.Account.find({"UserName":"keyword"}).count()
聚集查詢結果排序
>>> db.Account.find().sort("UserName") --默認爲升序
>>> db.Account.find().sort("UserName",pymongo.ASCENDING) --升序
>>> db.Account.find().sort("UserName",pymongo.DESCENDING) --降序
聚集查詢結果多列排序
>>> db.Account.find().sort([("UserName",pymongo.ASCENDING),("Email",pymongo.DESCENDING)])
添加記錄
>>> db.Account.insert({"AccountID":21,"UserName":"libing"})
修改記錄
>>> db.Account.update({"UserName":"libing"},{"$set":{"Email":"[email protected]","Password":"123"}})
刪除記錄
>>> db.Account.remove() -- 全部刪除
>>> db.Test.remove({"UserName":"keyword"})
python webpy相關
webpy3.0模板中文文檔
http://webpy.org/docs/0.3/templetor.zh-cn
webpy3.0獲取上傳的文件:
http://webpy.org/cookbook/fileupload.zh-cn
params = web.input(myfile={})
with open(filedir + '/' + filename, 'wb') as f_out:
f_out.write(params['myfile'].file.read())
f_out.close()
指定404結果
web.notfound()
python相關
一. 使用md5包
import md5
src = 'this is a md5 test.'
m1 = md5.new()
m1.update(src)
print m1.hexdigest()
二. 使用hashlib
import hashlib
m2 = hashlib.md5()
m2.update(src)
print m2.hexdigest()
推薦使用第二種方法。
http://outofmemory.cn/code-snippet/939/python-liangzhong-produce-md5-method
時間格式化輸出:
time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
strftime(format[, tuple]) -> string
將指定的struct_time(默認爲當前時間),根據指定的格式化字符串輸出
python中時間日期格式化符號:
%y 兩位數的年份表示(00-99)
%Y 四位數的年份表示(000-9999)
%m 月份(01-12)
%d 月內中的一天(0-31)
%H 24小時制小時數(0-23)
%I 12小時制小時數(01-12)
%M 分鐘數(00=59)
%S 秒(00-59)
%a 本地簡化星期名稱
%A 本地完整星期名稱
%b 本地簡化的月份名稱
%B 本地完整的月份名稱
%c 本地相應的日期表示和時間表示
%j 年內的一天(001-366)
%p 本地A.M.或P.M.的等價符
%U 一年中的星期數(00-53)星期天爲星期的開始
%w 星期(0-6),星期天爲星期的開始
%W 一年中的星期數(00-53)星期一爲星期的開始
%x 本地相應的日期表示
%X 本地相應的時間表示
%Z 當前時區的名稱
%% %號本身
python文件操作:
>>> os.path.exists('d:/assist/getTeacherList.py')
True
>>> os.path.isfile('d:/assist')
False
>>> os.path.isfile('d:/assist/getTeacherList.py')
True
>>> os.makedirs('d:/assist/set')
字符串是否包含:
my_string = "abcdef"
if "abc" in my_string:
has_abc = True
字符串轉整數:
http://blog.csdn.net/tweller/article/details/7767538
import string
tt='555'
ts=string.atoi(tt)
附錄:
mongo命令:http://blog.csdn.net/delbboy/article/details/7611715