1

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()
Cœur
  • 37,241
  • 25
  • 195
  • 267
XaBla
  • 127
  • 1
  • 1
  • 8
  • 1
    What exactly is the problem? How to open a file? How to read a line from a file? How to parse a string as a timestamp? – mkrieger1 Oct 17 '18 at 09:55
  • https://stackoverflow.com/questions/79797/how-do-i-convert-local-time-to-utc-in-python Has this Question not be answered under this post?? – Michael Maguire Oct 17 '18 at 09:55
  • @MichaelMaguire I solved the problem to convert from LOCAL to UTC time, you can find in my question the code I used. – XaBla Oct 17 '18 at 11:29
  • @mkrieger1 the problem is that the variable utc_dt has a different format from the onw in the file, so I don't kown how to compare them – XaBla Oct 17 '18 at 11:30

2 Answers2

1

This works:

import datetime
import pytz
now = datetime.datetime.utcnow()
now.isoformat()
Rahul Agarwal
  • 4,034
  • 7
  • 27
  • 51
  • Hi, thanks for your answer, the problem with your solution doesn't compare the result in the variable `utc_dt` with all the rows in the file. Probably I explained myself badly. I want to compare the variable `utc_dt` with all the rows in the file, but there's the problem that they have a different format, as you can see in my question – XaBla Oct 17 '18 at 11:35
  • I have a file with only one column in which there is something like `2018-07-30T15:20:36.524281125`, what changes from row to row is the date and the time (which is a UTC time). The problem is that what the variable `utc_dt` returns, if I print it, is something like `2018-07-30 21:15:00+00:00`. Has you can see the format are different, so I don't know how to do compare them. Tell me if I have not explained myself well! – XaBla Oct 17 '18 at 11:51
  • I tried your solution, and to verify I did: `u=utc_dt.isoformat() print 'utc_dt = ', utc_dt` that returns `utc_dt = 2018-07-30 21:15:00+00:00` then `print 'utc_dt.isoformat= ', utc_dt.isoformat()` which returns `utc_dt.isoformat= 2018-07-30T21:15:00+00:00`, then `open('file.txt', 'r') as file: for line in file: if u in line: print line file.close()` but it doesn't enter the if cycle because it doesn't match with any row in the file. I edit my question with the file I have so that you can have a more clear idea of what I have to check in the file. – XaBla Oct 17 '18 at 12:23
  • 1
    Ok, I menaged to solve it and I edited my question with the solution. Thanks a lot for your help! – XaBla Oct 17 '18 at 13:08
0

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
veri_pudicha_coder
  • 1,411
  • 9
  • 9
  • Hi, thaks for your aswer, I was thinking to do something similar, but the problem is that the `utc_dt` format is different from the one in the file – XaBla Oct 17 '18 at 11:32