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
#1
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))
Reply
#2
on lines 10-11, is it c[:2] or n[:2] and c[2] or [n2]?
Also, could you please show some sample data
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#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
#4
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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']))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(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!!
Reply
#7
Just noticed a mistake in the format string
'%Y-%m-%d %H%M%S.%f' should be '%Y-%m-%d %H:%M:%S.%f'
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
(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.
Reply
#9
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...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

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