gpt4 book ai didi

python - 将字符串转换为 ISO 日期 python

转载 作者:行者123 更新时间:2023-11-29 13:23:12 26 4
gpt4 key购买 nike

我正在使用 postgresql。在它上面我有我必须在 python 上使用的数据。一个表字段包含一个日期。它看起来像这样:

enter image description here

在我的 python 脚本上使用 psycopg2 我得到了结果,当我得到日期时它被保存为 mongodb 上的字符串

"created" : "2016-06-16T19:53:43.776456"

我的代码是这样的:

query = "SELECT name, created FROM user;"
cur.execute(query)
user_data = cur.fetchall()
for user_info in user_data:
user_json = {
'name': user_info[0],
'created': user_info[1]
}

我想将此 "created": "2016-06-16T19:53:43.776456" 转换为 "created": ISODate("2016-06-24T09:08:09.333 Z"),但我不知道如何将其转换为正确的格式。请帮忙!

最佳答案

在我看来,当您将数据存储到 MongoDB 中时,您不需要指定 ISODate

Python代码(片段):

cur.execute("select name, created from tt_user;")
data = cur.fetchall()
for row in data:
print(row[0], row[1])
mdr = {
'user': row[0],
'created': row[1]
}
mongo_db.tt_user.insert_one(mdr)
print("Done.")

print("Check data in MongoDB");

for row in mongo_db.tt_user.find():
print(row)

及其输出:

Load data from PostgreSQL into MongoDB...
('user1', datetime.datetime(2016, 6, 29, 13, 26, 28, 632436, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)))
('user2', datetime.datetime(2016, 6, 29, 13, 26, 28, 632739, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)))
('user3', datetime.datetime(2016, 6, 29, 13, 26, 28, 632751, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)))
('user4', datetime.datetime(2016, 6, 29, 13, 26, 28, 632757, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)))
('user5', datetime.datetime(2016, 6, 29, 13, 26, 28, 632762, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)))
Done.
Check data in MongoDB....
{u'_id': ObjectId('5773a254afd34b43d76aa792'), u'user': u'user1', u'created': datetime.datetime(2016, 6, 29, 10, 26, 28, 632000)}
{u'_id': ObjectId('5773a255afd34b43d76aa793'), u'user': u'user2', u'created': datetime.datetime(2016, 6, 29, 10, 26, 28, 632000)}
{u'_id': ObjectId('5773a255afd34b43d76aa794'), u'user': u'user3', u'created': datetime.datetime(2016, 6, 29, 10, 26, 28, 632000)}
{u'_id': ObjectId('5773a255afd34b43d76aa795'), u'user': u'user4', u'created': datetime.datetime(2016, 6, 29, 10, 26, 28, 632000)}
{u'_id': ObjectId('5773a255afd34b43d76aa796'), u'user': u'user5', u'created': datetime.datetime(2016, 6, 29, 10, 26, 28, 632000)}

在 MongoDB 控制台中:

> db.tt_user.find()
{ "_id" : ObjectId("5773a254afd34b43d76aa792"), "user" : "user1", "created" : ISODate("2016-06-29T10:26:28.632Z") }
{ "_id" : ObjectId("5773a255afd34b43d76aa793"), "user" : "user2", "created" : ISODate("2016-06-29T10:26:28.632Z") }
{ "_id" : ObjectId("5773a255afd34b43d76aa794"), "user" : "user3", "created" : ISODate("2016-06-29T10:26:28.632Z") }
{ "_id" : ObjectId("5773a255afd34b43d76aa795"), "user" : "user4", "created" : ISODate("2016-06-29T10:26:28.632Z") }
{ "_id" : ObjectId("5773a255afd34b43d76aa796"), "user" : "user5", "created" : ISODate("2016-06-29T10:26:28.632Z") }
>

所以这是客户端软件的行为,而不是存储问题。只需将数据保存为日期时间即可。

PS:谢谢你最后强制我安装 MongoDB :)

关于python - 将字符串转换为 ISO 日期 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38087666/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com