Reputation: 127
I have a file like this:
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
the time in the file in UCT time. What I want to do is: given a date and a time in LOCAL time, convert it in UTC time, and then search in the file the correspondace.
What I've done is:
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
but now I don't know how to check utc_dt
in the file.
Can someone help me please? Thank you!!
[SOLUTION]
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()
Upvotes: 1
Views: 72
Reputation: 4110
This works:
import datetime
import pytz
now = datetime.datetime.utcnow()
now.isoformat()
Upvotes: 1
Reputation: 1511
You can just read the file and check if the given time (utc_dt) is present in the file.
with open('filename', 'r') as file:
for line in file:
if str(utc_dt) in line:
print line
Upvotes: 0