Python Forum
time zone converting with list slicing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
time zone converting with list slicing
#3
(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.
Reply


Messages In This Thread
RE: time zone converting with list slicing - by isabelle - May-22-2018, 11:23 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020