gpt4 book ai didi

python - 检查文件中的数据

转载 作者:行者123 更新时间:2023-12-01 08:54:46 27 4
gpt4 key购买 nike

我有一个这样的文件:

0 2018-07-30T15:20:36.524281125
1 2018-07-30T15:21:12.821653375
2 2018-07-30T15:21:29.271170975
3 2018-07-30T15:21:30.381507275
4 2018-07-30T16:13:05.816863525
5 2018-07-30T16:13:06.958256975
6 2018-07-30T16:13:08.082365950
7 2018-07-30T16:13:09.221720400
8 2018-07-30T16:13:10.359005400
9 2018-07-30T16:13:11.487593100
10 2018-07-30T16:13:12.626307750
11 2018-07-30T16:13:13.749659475
12 2018-07-30T16:13:14.811689075
13 2018-07-30T16:13:15.870184700
14 2018-07-30T16:13:16.925621250
15 2018-07-30T16:13:17.983229825
16 2018-07-30T16:13:19.037089050
17 2018-07-30T16:13:20.094517175
18 2018-07-30T16:13:21.157441875
19 2018-07-30T16:13:22.220711325
20 2018-07-30T16:13:23.287695625
21 2018-07-30T16:13:24.349589400
22 2018-07-30T16:13:25.464690475
23 2018-07-30T16:13:26.499291350
24 2018-07-30T16:13:27.536470625
25 2018-07-30T16:13:28.579000175
26 2018-07-30T16:13:29.619571550
27 2018-07-30T16:13:30.657574450
28 2018-07-30T16:13:31.696135250
29 2018-07-30T16:13:32.736722050
30 2018-07-30T16:13:33.775218175
31 2018-07-30T16:13:34.812208750
32 2018-07-30T16:13:35.865376075
33 2018-07-30T16:13:36.906469075
34 2018-07-30T16:13:37.946010425
35 2018-07-30T16:13:38.983954225
36 2018-07-30T16:13:40.027211650
37 2018-07-30T16:13:41.069455950
38 2018-07-30T16:13:42.111004375
39 2018-07-30T16:13:43.159038875
40 2018-07-30T16:13:44.200659625

文件中的时间(UCT 时间)。我想要做的是:给定一个本地时间的日期和时间,将其转换为UTC时间,然后在文件中搜索对应的内容。

我所做的是:

import pytz, datetime

local = pytz.timezone ("Europe/Zurich")
naive = datetime.datetime.strptime ("2018-7-30 23:15:00", "%Y-%m-%d %H:%M:%S")
local_dt = local.localize(naive, is_dst=None)
utc_dt = local_dt.astimezone(pytz.utc)
print utc_dt

但现在我不知道如何检查文件中的utc_dt

有人可以帮我吗?谢谢!!

[解决方案]

import pytz, datetime
import re
from dateutil.parser import parse

local = pytz.timezone ("Europe/Zurich")
naive = datetime.datetime.strptime ("2018-7-30 23:15:00", "%Y-%m-%d %H:%M:%S")
local_dt = local.localize(naive, is_dst=None)
utc_dt = local_dt.astimezone(pytz.utc)
print 'utc_dt = ', utc_dt

utc_fileFormat=utc_dt.isoformat()
print 'utc_dt.isoformat() = ', utc_fileFormat

string=parse(utc_fileFormat)
print string

new_string=string.strftime('%Y-%m-%dT%H:%M:%S')
if new_string.endswith('+00:00'):
new_string = new_string[:-6]
print new_string

with open('fill_6999_B1_timestamps.txt', 'r') as file:
for line in file:
if new_string in line:
print line
file.close()

最佳答案

这有效:

import datetime
import pytz
now = datetime.datetime.utcnow()
now.isoformat()

关于python - 检查文件中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52851964/

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