Re: get the min date from a list
On Fri, Aug 15, 2014 at 12:21 PM, Denis McMahon wrote: > Output is a sorted list of the actual times and the UTC equivalents of > all the times in the original list. Note that I had to edit several > strings in your times list to ensure they were all in identical format: I > added leading 0s to numeric values in some strings, deleted extra spaces > in some strings, deleted extraneous information after the tz offset in > some strings. When feeding strings to a parsing function such as strptime > () it is critically important that the format specifier matches the input > data. >>> datetime.strptime("Mon, 9\t\t\tAug\n2014\r7:36:46\f-0700", "%a, %d %b %Y >>> %H:%M:%S %z") datetime.datetime(2014, 8, 9, 7, 36, 46, tzinfo=datetime.timezone(datetime.timedelta(-1, 61200))) strptime doesn't seem to care about variations in whitespace as long as some is present, or missing leading zeroes (although it does throw an error if the time zone offset is only 3 digits). -- https://mail.python.org/mailman/listinfo/python-list
Re: get the min date from a list
On 15/08/2014 19:21, Denis McMahon wrote: On Thu, 14 Aug 2014 22:10:36 +0800, luofeiyu wrote: I finished it ,but how to make it into more pythonic way such as min (dates, key = converter) 1. If you don't learn to post properly, I'm going to stop trying to help you. I say old bean do be careful, I've been suffering nightmares having been accused of nagging, I wouldn't want you to suffer the same fate. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: get the min date from a list
On Thu, 14 Aug 2014 22:10:36 +0800, luofeiyu wrote: > I finished it ,but how to make it into more pythonic way such as min > (dates, key = converter) 1. If you don't learn to post properly, I'm going to stop trying to help you. 2. To user strptime, you need to have all the time strings in the same format. Your time strings are not all in the same format. 3. Consider the following code which works on python 3.2: #!/usr/bin/python3 from datetime import tzinfo, timedelta, datetime, timezone times=[ 'Sat, 09 Aug 2014 07:36:46 -0700', # rest of array here 'Tue, 05 Aug 2014 01:55:24 +', ] realtimes = [ datetime.strptime( x, "%a, %d %b %Y %H:%M:%S %z" ) for x in times ] realtimes.sort() utctimes = [ x.astimezone(timezone(timedelta(0))) for x in realtimes ] for i in range( len( realtimes ) ): print( realtimes[i], "==", utctimes[i] ) Output is a sorted list of the actual times and the UTC equivalents of all the times in the original list. Note that I had to edit several strings in your times list to ensure they were all in identical format: I added leading 0s to numeric values in some strings, deleted extra spaces in some strings, deleted extraneous information after the tz offset in some strings. When feeding strings to a parsing function such as strptime () it is critically important that the format specifier matches the input data. -- Denis McMahon, denismfmcma...@gmail.com -- https://mail.python.org/mailman/listinfo/python-list
Re: get the min date from a list
Mark Lawrence : > The rules here are quite clear, don't top post. If you can't be > bothered to adhere to the rules, don't post. Top-posting is bad, but I find nagging worse. Marko -- https://mail.python.org/mailman/listinfo/python-list
Re: get the min date from a list
On 15/08/2014 01:56, Dan Stromberg wrote: On Thu, Aug 14, 2014 at 5:44 PM, Mark Lawrence wrote: I really don't understand why people here are spoon feeding you when you still insist on top posting. Ever heard the term "manners"? Oh what a stupid comment, obviously not. *plonk* Getting people to stop top-posting is a losing battle. No it isn't a losing battle. The rules here are quite clear, don't top post. If you can't be bothered to adhere to the rules, don't post. Particularly when all of your questions could be answered by reading the £$%^ing docs. Aren't there more important things to worry about? Like the NSA overstepping or the Fergeson, Missouri police? Who or what are the NSA? Missouri, never heard of it, some tiny little hamlet on one of the Scottish islands? :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: get the min date from a list
On Thu, Aug 14, 2014 at 5:44 PM, Mark Lawrence wrote: > I really don't understand why people here are spoon feeding you when you > still insist on top posting. Ever heard the term "manners"? Oh what a > stupid comment, obviously not. > > *plonk* Getting people to stop top-posting is a losing battle. Aren't there more important things to worry about? Like the NSA overstepping or the Fergeson, Missouri police? -- https://mail.python.org/mailman/listinfo/python-list
Re: get the min date from a list
On 15/08/2014 00:22, luofeiyu wrote: I really don't understand why people here are spoon feeding you when you still insist on top posting. Ever heard the term "manners"? Oh what a stupid comment, obviously not. *plonk* -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: get the min date from a list
On Thu, Aug 14, 2014 at 5:22 PM, luofeiyu wrote: > I am glad to hear that it is no necessary to create a complicated my > function to change the time string. > But in my computer the timezone offset do not work for me. > I am in win7+python34. > import sys sys.version > '3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:25:23) [MSC v.1600 64 bit > (AMD64)]' > import time time.tzname > ('China Standard Time', 'China Daylight Time') time.strptime(t1,"%a, %d %b %Y %H:%M:%S %z") > time.struct_time(tm_year=2014, tm_mon=8, tm_mday=9, tm_hour=7, tm_min=36, > tm_sec > =46, tm_wday=5, tm_yday=221, tm_isdst=-1) time.strptime(t2,"%a, %d %b %Y %H:%M:%S %z") > time.struct_time(tm_year=2014, tm_mon=8, tm_mday=9, tm_hour=7, tm_min=36, > tm_sec > =46, tm_wday=5, tm_yday=221, tm_isdst=-1) > > The %z does not work for me, how to adjust it my computer? As I noted in my previous post, try using datetime.datetime.strptime instead. The time.strptime function depends on the C libraries to support it, while the datetime.strptime does not. -- https://mail.python.org/mailman/listinfo/python-list
Re: get the min date from a list
I am glad to hear that it is no necessary to create a complicated my function to change the time string. But in my computer the timezone offset do not work for me. I am in win7+python34. >>> import sys >>> sys.version '3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:25:23) [MSC v.1600 64 bit (AMD64)]' >>> import time >>> time.tzname ('China Standard Time', 'China Daylight Time') >>> time.strptime(t1,"%a, %d %b %Y %H:%M:%S %z") time.struct_time(tm_year=2014, tm_mon=8, tm_mday=9, tm_hour=7, tm_min=36, tm_sec =46, tm_wday=5, tm_yday=221, tm_isdst=-1) >>> time.strptime(t2,"%a, %d %b %Y %H:%M:%S %z") time.struct_time(tm_year=2014, tm_mon=8, tm_mday=9, tm_hour=7, tm_min=36, tm_sec =46, tm_wday=5, tm_yday=221, tm_isdst=-1) >>> The %z does not work for me, how to adjust it my computer? On 8/15/2014 1:24 AM, Ian Kelly wrote: On Thu, Aug 14, 2014 at 8:10 AM, luofeiyu wrote: I finished it ,but how to make it into more pythonic way such as min (dates, key = converter) The converter will be your changeToUnix function, but you'll need to rework it to convert a single, rather than the whole list. def changeToUnix(times): import time,calendar,re time_list=[] for time1 in times: pat='(.+?)([-|+]\d{4})(\(?.*\)?)' x=re.search(pat,time1) time_part=x.groups()[0].strip() tz_part=x.groups()[1] tz_acc=x.groups()[2].strip().replace('(','').replace(')','') num=int(tz_part[1:3]) if tz_acc in ["","UTC","CST","GMT","EST","CST","PST"]: num=num if tz_acc in ["EDT"]: num=num+2 if tz_acc in ["CDT"]: num=num+1 if tz_acc in ["PDT"]: num=num-1 op=tz_part[0] y=time.strptime(time_part,"%a, %d %b %Y %H:%M:%S") if op=="-":hour=int(y.tm_hour)-num if op=="+":hour=int(y.tm_hour)+num time2=(y.tm_year,y.tm_mon,y.tm_mday,hour,y.tm_min,y.tm_sec) time_list.append(calendar.timegm(time2)) return(time_list) This looks way overly complicated. Why are you trying to mess with the time zone offset? strptime has a %z format code for parsing that (although I would recommend using datetime.datetime.strptime since I'm not sure how well time.strptime supports it). By adding the offset to the hour like that, you could end up with an hour that falls outside the accepted range. And I think you have your addition and subtraction switched around anyway -- in effect you're doubling the time zone offset, not converting to UTC. Also you would be losing the minutes part of the time zone offset if you were to get something like +0545. I also don't understand why you're special-casing and modifying three of the time zones. All you need to do is strip the parenthesized timezone off the string if it's present, and pass the result to datetime.datetime.strptime. -- https://mail.python.org/mailman/listinfo/python-list
Re: get the min date from a list
On Thu, Aug 14, 2014 at 8:10 AM, luofeiyu wrote: > I finished it ,but how to make it into more pythonic way such as > > min (dates, key = converter) The converter will be your changeToUnix function, but you'll need to rework it to convert a single, rather than the whole list. > def changeToUnix(times): > import time,calendar,re > time_list=[] > for time1 in times: > pat='(.+?)([-|+]\d{4})(\(?.*\)?)' > x=re.search(pat,time1) > time_part=x.groups()[0].strip() > tz_part=x.groups()[1] > tz_acc=x.groups()[2].strip().replace('(','').replace(')','') > num=int(tz_part[1:3]) > if tz_acc in ["","UTC","CST","GMT","EST","CST","PST"]: num=num > if tz_acc in ["EDT"]: num=num+2 > if tz_acc in ["CDT"]: num=num+1 > if tz_acc in ["PDT"]: num=num-1 > op=tz_part[0] > y=time.strptime(time_part,"%a, %d %b %Y %H:%M:%S") > if op=="-":hour=int(y.tm_hour)-num > if op=="+":hour=int(y.tm_hour)+num > time2=(y.tm_year,y.tm_mon,y.tm_mday,hour,y.tm_min,y.tm_sec) > time_list.append(calendar.timegm(time2)) > return(time_list) This looks way overly complicated. Why are you trying to mess with the time zone offset? strptime has a %z format code for parsing that (although I would recommend using datetime.datetime.strptime since I'm not sure how well time.strptime supports it). By adding the offset to the hour like that, you could end up with an hour that falls outside the accepted range. And I think you have your addition and subtraction switched around anyway -- in effect you're doubling the time zone offset, not converting to UTC. Also you would be losing the minutes part of the time zone offset if you were to get something like +0545. I also don't understand why you're special-casing and modifying three of the time zones. All you need to do is strip the parenthesized timezone off the string if it's present, and pass the result to datetime.datetime.strptime. -- https://mail.python.org/mailman/listinfo/python-list
Re: get the min date from a list
luofeiyu : > y=time.strptime(time_part,"%a, %d %b %Y %H:%M:%S") As I said, whether that works depends on your locale -- according to the reference documentation. In practice, I couldn't get that to fail in my tests. I would be on my guard, though. That might mean I couldn't use strptime to convert the dates. Marko -- https://mail.python.org/mailman/listinfo/python-list
Re: get the min date from a list
On 14/08/2014 15:10, luofeiyu wrote: How many times do you have to be asked not to top post before the message sinks in? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: get the min date from a list
I finished it ,but how to make it into more pythonic way such as min (dates, key = converter) here is my code times=['Sat, 09 Aug 2014 07:36:46 -0700', 'Fri, 8 Aug 2014 22:25:40 -0400', 'Sat, 9 Aug 2014 12:46:43 +1000', 'Sat, 9 Aug 2014 12:50:52 +1000', 'Sat, 9 Aug 2014 02:51:01 + (UTC)', 'Sat, 9 Aug 2014 13:03:24 +1000', 'Sat, 09 Aug 2014 13:06:28 +1000', 'Fri, 8 Aug 2014 20:48:44 -0700 (PDT)', 'Fri, 8 Aug 2014 23:52:09 -0700 (PDT)', 'Sat, 09 Aug 2014 09:15:50 +0200', 'Sat, 9 Aug 2014 01:49:54 -0600', 'Sat, 9 Aug 2014 01:57:18 -0600', 'Sat, 9 Aug 2014 17:54:23 +0800 (CST)', 'Sat, 9 Aug 2014 12:49:08 +0200', 'Sat, 9 Aug 2014 07:31:09 -0400', 'Sat, 9 Aug 2014 07:34:16 -0400', 'Sat, 09 Aug 2014 11:39:16 +', 'Sat, 9 Aug 2014 07:40:41 -0400', 'Sat, 9 Aug 2014 11:46:54 +', 'Sat, 09 Aug 2014 13:48:17 +0200', 'Sat, 09 Aug 2014 21:53:11 +1000', 'Sat, 09 Aug 2014 14:13:13 +0200', 'Sat, 09 Aug 2014 08:16:08 -0400', 'Sat, 09 Aug 2014 22:17:25 +1000', 'Sat, 09 Aug 2014 14:33:54 +0200', 'Sat, 9 Aug 2014 14:46:01 +0200', 'Sat, 09 Aug 2014 10:34:58 -0300', 'Sat, 09 Aug 2014 11:34:22 -0400', 'Sat, 09 Aug 2014 12:16:56 -0400', 'Sat, 09 Aug 2014 12:26:38 -0400', 'Sat, 09 Aug 2014 13:29:59 -0400', 'Sat, 09 Aug 2014 13:43:33 -0400', 'Sat, 9 Aug 2014 11:30:35 -0300', 'Sat, 09 Aug 2014 20:14:20 +0200', 'Sun, 10 Aug 2014 08:18:34 +1000', 'Sat, 9 Aug 2014 18:23:08 -0400 (EDT)', 'Sat, 09 Aug 2014 18:30:17 -0400', 'Sat, 09 Aug 2014 18:31:38 -0400', 'Sun, 10 Aug 2014 09:43:44 +1000', 'Sat, 9 Aug 2014 18:27:15 -0700 (PDT)', 'Sun, 10 Aug 2014 03:44:56 +0200', 'Sun, 10 Aug 2014 01:55:24 + (UTC)', 'Sun, 10 Aug 2014 02:01:06 + (UTC)', 'Sat, 9 Aug 2014 19:41:08 -0700 (PDT)', 'Sat, 9 Aug 2014 22:51:29 -0400 (EDT)', 'Sun, 10 Aug 2014 07:34:44 +0200', 'Tue, 5 Aug 2014 01:55:24 + (UTC)'] def changeToUnix(times): import time,calendar,re time_list=[] for time1 in times: pat='(.+?)([-|+]\d{4})(\(?.*\)?)' x=re.search(pat,time1) time_part=x.groups()[0].strip() tz_part=x.groups()[1] tz_acc=x.groups()[2].strip().replace('(','').replace(')','') num=int(tz_part[1:3]) if tz_acc in ["","UTC","CST","GMT","EST","CST","PST"]: num=num if tz_acc in ["EDT"]: num=num+2 if tz_acc in ["CDT"]: num=num+1 if tz_acc in ["PDT"]: num=num-1 op=tz_part[0] y=time.strptime(time_part,"%a, %d %b %Y %H:%M:%S") if op=="-":hour=int(y.tm_hour)-num if op=="+":hour=int(y.tm_hour)+num time2=(y.tm_year,y.tm_mon,y.tm_mday,hour,y.tm_min,y.tm_sec) time_list.append(calendar.timegm(time2)) return(time_list) y=changeToUnix(times) times[y.index(min(y))] 'Tue, 5 Aug 2014 01:55:24 + (UTC)' You neglected to specify your Python version, but I'll assume at least 2.5. The min function did exactly as it's documented to do, found the minimum string, by alphabetical ordering. Since 'F' is less than 'T' it didn't need to look at the rest. If you had been sorting a list of datetime objects, it would have found the least of those. Your simplest answer is probably to write a function that converts a string like you have into a datetime object, say call it converter (). Then after testing it, you call min (dates, key = converter) Note I did NOT use parens on converter. I also used the name dates for the list, since it's a collection of dates. But that assumes you rename it in your code that gathered them. -- DaveA -- https://mail.python.org/mailman/listinfo/python-list
Re: get the min date from a list
In article , Dave Angel wrote: > Your simplest answer is probably to write a function that converts > a string like you have into a datetime object, say call it > converter (). Then after testing it, you call > > min (dates, key = converter) Wow, after all these years, I didn't know min() took a key argument. Of course, it makes sense, but I just never noticed that before. Thanks! And for the OP, for the converter function, I would suggest dateutil.parse.parser(), from the python-dateutil module (https://labix.org/python-dateutil). -- https://mail.python.org/mailman/listinfo/python-list
Re: get the min date from a list
On 10/08/2014 08:14, luofeiyu wrote: >>> date ['Sat, 09 Aug 2014 07:36:46 -0700', 'Fri, 8 Aug 2014 22:25:40 -0400', 'Sat, 9 Au g 2014 12:46:43 +1000', 'Sat, 9 Aug 2014 12:50:52 +1000', 'Sat, 9 Aug 2014 02:51 :01 + (UTC)', 'Sat, 9 Aug 2014 13:03:24 +1000', 'Sat, 09 Aug 2014 13:06:28 + 1000', 'Fri, 8 Aug 2014 20:48:44 -0700 (PDT)', 'Fri, 8 Aug 2014 23:52:09 -0700 ( PDT)', 'Sat, 09 Aug 2014 09:15:50 +0200', 'Sat, 9 Aug 2014 01:49:54 -0600', 'Sat , 9 Aug 2014 01:57:18 -0600', 'Sat, 9 Aug 2014 17:54:23 +0800 (CST)', 'Sat, 9 Au g 2014 12:49:08 +0200', 'Sat, 9 Aug 2014 07:31:09 -0400', 'Sat, 9 Aug 2014 07:34 :16 -0400', 'Sat, 09 Aug 2014 11:39:16 +', 'Sat, 9 Aug 2014 07:40:41 -0400', 'Sat, 9 Aug 2014 11:46:54 +', 'Sat, 09 Aug 2014 13:48:17 +0200', 'Sat, 09 A ug 2014 21:53:11 +1000', 'Sat, 09 Aug 2014 14:13:13 +0200', 'Sat, 09 Aug 2014 08 :16:08 -0400', 'Sat, 09 Aug 2014 22:17:25 +1000', 'Sat, 09 Aug 2014 14:33:54 +02 00', 'Sat, 9 Aug 2014 14:46:01 +0200', 'Sat, 09 Aug 2014 10:34:58 -0300', 'Sat, 09 Aug 2014 11:34:22 -0400', 'Sat, 09 Aug 2014 12:16:56 -0400', 'Sat, 09 Aug 201 4 12:26:38 -0400', 'Sat, 09 Aug 2014 13:29:59 -0400', 'Sat, 09 Aug 2014 13:43:33 -0400', 'Sat, 9 Aug 2014 11:30:35 -0300', 'Sat, 09 Aug 2014 20:14:20 +0200', 'S un, 10 Aug 2014 08:18:34 +1000', 'Sat, 9 Aug 2014 18:23:08 -0400 (EDT)', 'Sat, 0 9 Aug 2014 18:30:17 -0400', 'Sat, 09 Aug 2014 18:31:38 -0400', 'Sun, 10 Aug 2014 09:43:44 +1000', 'Sat, 9 Aug 2014 18:27:15 -0700 (PDT)', 'Sun, 10 Aug 2014 03:4 4:56 +0200', 'Sun, 10 Aug 2014 01:55:24 + (UTC)', 'Sun, 10 Aug 2014 02:01:06 + (UTC)', 'Sat, 9 Aug 2014 19:41:08 -0700 (PDT)', 'Sat, 9 Aug 2014 22:51:29 -0400 (EDT)', 'Sun, 10 Aug 2014 07:34:44 +0200', 'Tue, 5 Aug 2014 01:55:24 +000 0 (UTC)'] >>> min(date) 'Fri, 8 Aug 2014 20:48:44 -0700 (PDT)' The result is wrong,the min date should be 'Tue, 5 Aug 2014 01:55:24 +000 0 (UTC)' ,how can i get it ? The result is probably correct (I haven't checked) as you're comparing strings. I'll leave you to read the docs to find out how to convert the strings to datetimes and compare them. Start here https://docs.python.org/3/ as that way you'll get used to finding your way around them. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list