Posts: 4
Threads: 1
Joined: May 2018
May-22-2018, 10:46 AM
(This post was last modified: May-22-2018, 10:54 AM by buran.)
Hi! I struggle with converting a list with time and date to another timezone. The original list contains some data that I don't want (movements) so that's what the slicing is about.
This is what I tried, but it dosen't work. There's maybe a better way to do it, in that case, please let me know!
The DateAndTime list comes in this format: [['2016-01-16', '17:22:10.171150'], ['2016-01-16', '17:22:10.171150'], ['2016-01-16', '17:22:10.171150'], ['2016-01-16', '17:22:10.171150']]
Appreciate all help!
from datetime import datetime
import pytz
birdfile=open('birds.dat')
def timezone(data):
movements=[]
DateAndTime=[]
for n in enumerate(data):
DateAndTime.append(c[:2])
movements.append(c[2])
SweTime=[]
for m in len(DateAndTime):
x=datetime(DateAndTime[m],pytz.timezone('Europe/London'))
yl=x.astimezone(pytz.timezone('Europe/Stockholm'))
SweTime.append(yl)
return SweTime
print(timezone(birdfile))
Posts: 8,160
Threads: 160
Joined: Sep 2016
on lines 10-11, is it c[:2] or n[:2] and c[2] or [n2]?
Also, could you please show some sample data
Posts: 4
Threads: 1
Joined: May 2018
May-22-2018, 11:23 AM
(This post was last modified: May-22-2018, 11:25 AM by buran.)
(May-22-2018, 10:46 AM)isabelle Wrote: Hi! I struggle with converting a list with time and date to another timezone. The original list contains some data that I don't want (movements) so that's what the slicing is about. This is what I tried, but it dosen't work. There's maybe a better way to do it, in that case, please let me know! The DateAndTime list comes in this format: [['2016-01-16', '17:22:10.171150'], ['2016-01-16', '17:22:10.171150'], ['2016-01-16', '17:22:10.171150'], ['2016-01-16', '17:22:10.171150']] Appreciate all help! from datetime import datetime import pytz birdfile=open('birds.dat') def timezone(data): movements=[] DateAndTime=[] for n in enumerate(data): DateAndTime.append(c[:2]) movements.append(c[2]) SweTime=[] for m in len(DateAndTime): x=datetime(DateAndTime[m],pytz.timezone('Europe/London')) yl=x.astimezone(pytz.timezone('Europe/Stockholm')) SweTime.append(yl) return SweTime print(timezone(birdfile))
I don't know about lines 10-11 but I've tried it and it works on its own, it's when I try to convert the whole list it becomes difficult.
Here are part of the code and result if it was this you meant.
def timezone(birdfile):
movements=[]
DateAndTime=[]
for n in enumerate(birdfile):
DateAndTime.append(c[:2])
movements.append(c[2])
return DateAndTime, movements
print(DateAndTime[:4], movements[:4]) and the results are:
[['2015-01-25', '14:05:41.274647'], ['2015-01-25', '14:08:05.036915'], ['2015-01-25', '14:10:05.536604'], ['2015-01-25', '14:12:05.142511']] ['70', '70', '70', '70']
since I just prints the 4 first elements in the list. the four 70 in the end is movements and has nothing to do with the task I'm trying to do.
Posts: 8,160
Threads: 160
Joined: Sep 2016
Obviously I cannot test, but I don't think this code should work:
for n in enumerate(birdfile):
DateAndTime.append(c[:2])
movements.append(c[2]) c is not defined!!!
I will post the time zone conversion shortly
Posts: 8,160
Threads: 160
Joined: Sep 2016
May-22-2018, 11:49 AM
(This post was last modified: May-22-2018, 11:49 AM by buran.)
something like this
from datetime import datetime
import pytz
def lon2swe(date_time):
london_datetime_tzunaware = datetime.strptime(' '.join(date_time), '%Y-%m-%d %H%M%S.%f')
london_datetime = london_datetime_tzunaware.replace(tzinfo = pytz.timezone('Europe/London'))
swedish_datetime = london_datetime.astimezone(pytz.timezone('Europe/Stockholm'))
return swedish_datetime
print(lon2swe(['2016-01-16', '17:22:10.171150']))
Posts: 4
Threads: 1
Joined: May 2018
(May-22-2018, 11:49 AM)buran Wrote: something like this from datetime import datetime import pytz def lon2swe(date_time): london_datetime_tzunaware = datetime.strptime(' '.join(date_time), '%Y-%m-%d %H%M%S.%f') london_datetime = london_datetime_tzunaware.replace(tzinfo = pytz.timezone('Europe/London')) swedish_datetime = london_datetime.astimezone(pytz.timezone('Europe/Stockholm')) return swedish_datetime print(lon2swe(['2016-01-16', '17:22:10.171150'])) Thanks a lot!!
Posts: 8,160
Threads: 160
Joined: Sep 2016
Just noticed a mistake in the format string
'%Y-%m-%d %H%M%S.%f' should be '%Y-%m-%d %H:%M:%S.%f'
Posts: 4
Threads: 1
Joined: May 2018
May-24-2018, 06:59 AM
(This post was last modified: May-24-2018, 06:59 AM by isabelle.)
(May-23-2018, 10:58 AM)buran Wrote: Just noticed a mistake in the format string '%Y-%m-%d %H%M%S.%f' should be '%Y-%m-%d %H:%M:%S.%f' Thanks a lot!
I have trouble too now with this code:
for n in enumerate(birdfile):
DateAndTime.append(c[:2])
movements.append(c[2]) as you said, name 'c' is not defined. do you have any tip on how to fix it?
I tried with n and that doesn't work either.
Posts: 8,160
Threads: 160
Joined: Sep 2016
May-24-2018, 07:18 AM
(This post was last modified: May-24-2018, 07:18 AM by buran.)
can you show sample birdfile data?
where c comes from (i.e. what you expect it to be?) Note that most natural is tho guess you want to have
for n,c in enumerate(birdfile):
DateAndTime.append(c[:2])
movements.append(c[2]) where n will be the index/row number (result of enumerate) and c, whatever birdfile row looks like. However I guess you need to do something else with the data in the file (e.g. split row by comma or something). Thtat is why I continue to ask for sample data file. Also from your code so far I don't see the need for using enumerate at all...
Posts: 8,160
Threads: 160
Joined: Sep 2016
May-24-2018, 09:38 AM
(This post was last modified: May-24-2018, 09:38 AM by buran.)
So, given this thread https://python-forum.io/Thread-Plotting-...ver-a-year would you give us a broader perspective? Is this a school/university assignment? Obviously both of you tend to use this for n in enumerate(self.data):
EDIT: I would appreciate if you post the full text of the assignment and your dat file.
|