快速解決pymongo操作mongodb的時區(qū)問題
如下所示:
1、mongodb的日期時間格式是UTC時間,中國時間 = UTC時間 +8
2、可在pymongo客戶端加入時區(qū)以解決此問題:
import pytz
from pymongo import MongoClient
from datetime import datetime
tzinfo = pytz.timezone('Asia/Shanghai')
client = MongoClient(
host="127.0.0.1",
port=27017,
username="root",
password="123456",
authSource="admin", # 在哪個數(shù)據(jù)庫進(jìn)行身份驗證,默認(rèn)是admin
tz_aware=True, # 設(shè)置為True
tzinfo=tzinfo # 加入時區(qū)信息
)
db = client["test"]
collection = db["mytest"]
datetime.now() # 2020-04-11 10:42:42.452433
ret = collection.insert_one({
"name": "測試5",
"create_time": tzinfo.localize(datetime.now())
})
# create_time不能使用datetime.now()獲取時間,
# 應(yīng)該使用 datetime.utcnow()或 tzinfo.localize(datetime.now())或 datetime.now(tz=tzinfo)
# 這樣讀取數(shù)據(jù)時日期時間才是標(biāo)準(zhǔn)的中國時間
res = collection.find_one({"name": "測試5"})
print(res)
# {'_id': ObjectId('5e912ea261d252f76350728a'), 'name': '測試5', 'create_time': datetime.datetime(2020, 4, 11, 10, 42, 42, 452000, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>)}
# 下面測試直接使用datetime.now()的情形
datetime.now() # 2020-04-11 10:49:41.899445
collection.insert_one({
"name": "測試6",
"create_time": datetime.now()
})
res = collection.find_one({"name": "測試6"})
# {'_id': ObjectId('5e913045143015041d776d08'), 'name': '測試6', 'create_time': datetime.datetime(2020, 4, 11, 18, 49, 41, 899000, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>)}
# 可以看到時間+8小時
補(bǔ)充知識:pymongo 按照時間查詢
我就廢話不多說了,大家還是直接看代碼吧~
from pymongo import MongoClient
client=MongoClient('localhost',27017)
db=client.adv.message
from datetime import datetime
import pandas as pd
#db.insert_one({'player_id':js2['player_id'],'message':js2['message'],
# 'label':label,'predict_time':datetime.datetime.now()})
#按照時間查詢
q1={"predict_time":{"$gte" :datetime(2019,9,25) ,"$lte": datetime(2019,9,28)}}
l1=list(db.find(q1))
#l1=list(db.find({}))
df=pd.DataFrame(l1)
以上這篇快速解決pymongo操作mongodb的時區(qū)問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python 實現(xiàn)將大圖切片成小圖,將小圖組合成大圖的例子
這篇文章主要介紹了Python 實現(xiàn)將大圖切片成小圖,將小圖組合成大圖的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
python taipy庫輕松地將數(shù)據(jù)和機(jī)器學(xué)習(xí)模型轉(zhuǎn)為功能性Web應(yīng)用
taipy 是一個開源的 Python 庫,任何具有基本 Python 技能的人都可以使用,對于數(shù)據(jù)科學(xué)家、機(jī)器學(xué)習(xí)工程師和 Python 程序員來說,它是一個方便的工具,借助 Taipy,你可以輕松地將數(shù)據(jù)和機(jī)器學(xué)習(xí)模型轉(zhuǎn)變?yōu)楣δ苄缘?nbsp;Web 應(yīng)用程序2024-01-01
使用python實現(xiàn)簡單爬取網(wǎng)頁數(shù)據(jù)并導(dǎo)入MySQL中的數(shù)據(jù)庫
這篇文章主要為大家詳細(xì)介紹了如何使用 python 實現(xiàn)簡單爬取網(wǎng)頁數(shù)據(jù)并導(dǎo)入 MySQL 中的數(shù)據(jù)庫,對我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-06-06
TensorFlow安裝及jupyter notebook配置方法
下面小編就為大家?guī)硪黄猅ensorFlow安裝及jupyter notebook配置方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
win10下opencv-python特定版本手動安裝與pip自動安裝教程
這篇文章主要介紹了win10下opencv-python特定版本手動安裝與pip自動安裝教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
django數(shù)據(jù)模型on_delete, db_constraint的使用詳解
這篇文章主要介紹了django數(shù)據(jù)模型on_delete, db_constraint的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Python生成隨機(jī)數(shù)組的方法小結(jié)
這篇文章主要介紹了Python生成隨機(jī)數(shù)組的方法,結(jié)合實例形式總結(jié)分析了Python使用random模塊生成隨機(jī)數(shù)與數(shù)組操作相關(guān)技巧,需要的朋友可以參考下2017-04-04
python深度學(xué)習(xí)tensorflow實例數(shù)據(jù)下載與讀取
這篇文章主要為大家介紹了python深度學(xué)習(xí)tensorflow實例數(shù)據(jù)下載與讀取示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06

