结合日期时间和毫秒

恩萨胡

我在x.db文件中有一个表,它有两列。一列是 type datetime,另一列是 type millisecond我需要在 python 中将两列作为一列读取。

此处附有示例日期时间和毫秒

我想阅读datetime(这将是我x对情节的价值)作为

2022-05-25 14:20:26.948

2022-05-25 14:20:26.968

2022-05-25 14:20:26.988

2022-05-25 14:20:27.008

2022-05-25 14:20:26.028

2022-05-25 14:20:26.048

所以我可以以毫秒为单位绘制所需的数据。

我正在尝试构建的代码在这里

conn = sqlite3.connect('./x.db')

cur = conn.cursor()


cur.execute('select * from data ')
tmp_row = cur.fetchall()
#print(tmp_row)

conn.close()
xdate=[]
abc=[]
dfg=[]


for row in tmp_row:
    tmp=row[0]
    yy=tmp[0]+tmp[1]+tmp[2]+tmp[3]
    mm=tmp[5]+tmp[6]
    dd=tmp[8]+tmp[9]
    HH=tmp[11]+tmp[12]
    MM=tmp[14]+tmp[15]
    ss=tmp[17]+tmp[18]

    tmp2=row[1]
    print(tmp2)
    t=tmp2[0]+tmp2[1]+tmp2[2]



    tmpdt=dd+"/"+mm+"/"+yy+" "+HH+":"+MM+":"+ss+"."+t
    #print(tmpdt)
   
    #print(tmpdt)

我得到的错误是

t=tmp2[0]+tmp2 1 +tmp2[2] TypeError: 'int' object is not subscriptable

另一个问题是有些具有个位数的值。我需要00在连接 timestamp_S t_ms 时在前面添加

2022-05-25 14:20:27    8

要求是

2022-05-25 14:20:27.008
鸟虎

你可以使用datetime.datetime.strptimedatetime.datetime.replace函数来得到你想要的。

from datetime import datetime

for row in tmp_row:
    t = datetime.strptime(row[0], "%Y-%m-%d %H:%M:%S")  # t = datetime(2022, 5, 25, 14, 20, 26)
    t = t.replace(microsecond = int(row[1]) * 1000)     # t = datetime(2022, 5, 25, 14, 20, 26, 948000)
    print(str(t)[:23])) # str(t) = "2022-05-25 14:20:26.948000", str(t)[:23] = "2022-05-25 14:20.26.948"

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章