Python Forum

Full Version: time zone converting with list slicing
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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))
on lines 10-11, is it c[:2] or n[:2] and c[2] or [n2]?
Also, could you please show some sample data
(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.
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
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']))
(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!!
Just noticed a mistake in the format string
'%Y-%m-%d %H%M%S.%f' should be '%Y-%m-%d %H:%M:%S.%f'
(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.
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...
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.