I want to parse a date string, for example '2005-09-23', and since I haven't
done this before I would like to ask what is the best way to do it.
I've looked around and the dateutil seems to be what most people use, but
unfortunately I only get an empty file when I try to download it. I also
tried the standard modules and ended up with this
import datetime
from time import strptime
d = '2005-09-23'
w = strptime(d,'%Y-%m-%d')
print datetime.date( w[0], w[1], w[2] )
But I suspect there is a better way to do it?? 6 1947
Better than a single line of code? What is it that
you are looking for?
If you dates are consistent you can do:
year, month, day=map(int, d.split('-'))
but I'm not sure it is "better". I guess it depends
on what you want to do with them after parsing.
-Larry Bates
Kalle Anke wrote: I want to parse a date string, for example '2005-09-23', and since I haven't done this before I would like to ask what is the best way to do it.
I've looked around and the dateutil seems to be what most people use, but unfortunately I only get an empty file when I try to download it. I also tried the standard modules and ended up with this
import datetime from time import strptime
d = '2005-09-23' w = strptime(d,'%Y-%m-%d') print datetime.date( w[0], w[1], w[2] )
But I suspect there is a better way to do it??
Kalle Anke wrote: I want to parse a date string, for example '2005-09-23', and since I haven't done this before I would like to ask what is the best way to do it.
I've looked around and the dateutil seems to be what most people use, but unfortunatel y I only get an empty file when I try to download it. I also tried the standard modules and ended up with this
import datetime from time import strptime
d = '2005-09-23' w = strptime(d,'%Y-%m-%d') print datetime.date( w[0], w[1], w[2] )
Well, thats not _to_ bad.
But I suspect there is a better way to do it??
There is...
I would check out "strftime"
With strftime you could do something like this...
# ------
print strftime("%Y-%m-%d")
# -----
or if you wanted to get the time in the past or future you could do
something like
# ------
tm = time.localtime( time.time() + ((60 * 60) * 24))
print strftime("%Y-%m-%d", tm)
# -----
Which gets the date of tommarow.
HTH,
Peter
On Fri, 23 Sep 2005 23:01:18 +0200, Larry Bates wrote
(in article <ht************ ********@comcas t.com>): but I'm not sure it is "better". I guess it depends on what you want to do with them after parsing.
Sorry, I should have been clearer. I want to parse the date and create a
'date object' that is a part of larger object (I'm parsing a text file that
represents the larger object and the date is a part of it).
So my question should probably be: is there a better way to parse the date
and generate the 'date object' than the two step
w = strptime(d,'%Y-%m-%d')
datetime.date( w[0], w[1], w[2] )
Since I'm new to many things in Python I'm trying to learn if there is a
"better" way of doing things.
jem
Kalle Anke wrote: On Fri, 23 Sep 2005 23:01:18 +0200, Larry Bates wrote:
but I'm not sure it is "better". I guess it depends on what you want to do with them after parsing.
Sorry, I should have been clearer. I want to parse the date and create a 'date object' that is a part of larger object (I'm parsing a text file that represents the larger object and the date is a part of it).
So my question should probably be: is there a better way to parse the date and generate the 'date object' than the two step
w = strptime(d,'%Y-%m-%d') datetime.date( w[0], w[1], w[2] )
Since I'm new to many things in Python I'm trying to learn if there is a "better" way of doing things.
You're still not defining what "better" means to you, so who can say?
Perhaps you think a single line would be "better"?
datetime.date(* time.strptime(d , '%Y-%m-%d')[:3])
-Peter
On Sat, 24 Sep 2005 16:06:06 +0200, Peter Hansen wrote
(in article <04************ *************** ***@powergate.c a>): Kalle Anke wrote: On Fri, 23 Sep 2005 23:01:18 +0200, Larry Bates wrote:
but I'm not sure it is "better". I guess it depends on what you want to do with them after parsing.
Sorry, I should have been clearer. I want to parse the date and create a 'date object' that is a part of larger object (I'm parsing a text file that represents the larger object and the date is a part of it).
So my question should probably be: is there a better way to parse the date and generate the 'date object' than the two step
w = strptime(d,'%Y-%m-%d') datetime.date( w[0], w[1], w[2] )
Since I'm new to many things in Python I'm trying to learn if there is a "better" way of doing things.
You're still not defining what "better" means to you, so who can say?
Perhaps you think a single line would be "better"?
datetime.date(* time.strptime(d , '%Y-%m-%d')[:3])
:-) It's not my day I think
Better (in this case) =
+ Being "pythonic"
+ Avoiding going through a second representation (the tuple)
if there is some way to create a date object directly.
jem
Kalle Anke wrote: Better (in this case) =
+ Being "pythonic"
+ Avoiding going through a second representation (the tuple) if there is some way to create a date object directly.
I think the plainest and simplest approach would be to create a
well-named function which does the necessary steps (_not_ in a single
line of code but with nice readable temporary variables) and call that
whenever you need it.
Simple utility functions that call a few standard library functions to
do a job like this are very "pythonic", and trying to avoid a second
representation when (as in this case) there's no obvious way to do it is
not pythonic. :-)
I'd say if you've checked datetime.date() and not found a factory
function or whatever which knows how to parse a string (as many of us
have), and if you don't want to download, install, and depend on a
third-party extension such as mxDateTime which do this already, then a
function to wrap the ugly details is a very nice way to proceed.
If you need to do this often, then you've got the beginnings of your own
utility module or package, and having those around is pretty "pythonic" too.
(An alternative would be to contribute a patch to add something like
this to the datetime module. I don't know if it would be accepted, but
at least it would be the true arbiters of "pythonicis m" who would be
judging the matter, not you or I. :-) )
-Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Gerrit Holl |
last post by:
Posted with permission from the author.
I have some comments on this PEP, see the (coming) followup to this message.
PEP: 321
Title: Date/Time Parsing and Formatting
Version: $Revision: 1.3 $
Last-Modified: $Date: 2003/10/28 19:48:44 $
Author: A.M. Kuchling <amk@amk.ca>
Status: Draft
Type: Standards Track
|
by: dont bother |
last post by:
Hey,
I have been trying to parse emails:
But I could not find any examples or snippets of
parsing emails in python from the documentation.
Google did not help me much too.
I am trying to understand the module 'email' and the
functions described there to parse email but seems
difficult.
Can anyone help me in locating some pointers or
snippets on this issue.
|
by: Willem Ligtenberg |
last post by:
I want to parse a very large (2.4 gig) XML file (bioinformatics ofcourse :))
But I have no clue how to do that. Most things I see read the entire xml
file at once. That isn't going to work here ofcourse.
So I would like to parse a XML file one record at a time and then be able
to store the information in another object.
How should I do that?
Thanks in advance,
|
by: Thomas W |
last post by:
I'm developing a web-application where the user sometimes has to enter
dates in plain text, allthough a format may be provided to give clues.
On the server side this piece of text has to be parsed into a datetime
python-object. Does anybody have any pointers on this?
Besides the actual parsing, my main concern is the different locale
date formats and how to be able to parse those strange us-like
"month/day/year" compared to the clever...
|
by: .:mmac:. |
last post by:
I have a bunch of files (Playlist files for media player) and I am trying to
create an automatically generated web page that includes the last 20 or 30
of these files. The files are created every week and are named XX-XX-XX.ASX
where the X's represent the date i.e. 05-22-05.asx
The files are a specific format and will always contain tags like the
following:
<TITLE>My media file title</TITLE>
<AUTHOR>Media file author</AUTHOR>
<Ref href =...
| |
by: bj0011 |
last post by:
I am trying to parse an XML file obtained from a public WebService (see below). This file does not seem to be loading into a .NET XmlDocument right (I think it is because of all of the <Item Name="" Type=""></Item> elements). I need to get out the data for the <Id/>, Name="Authors", Name="Title", and Name="PubDate" elements. Anyone have an idea how to do this (I have tried to get them out short of dumping the doc out to a string and parsing it...
|
by: Uncle Leo |
last post by:
I created an OleDbDataAdapter with the wizard in Visual Studio 2003.
It created a dataset, connectionstring etc. for me to work with. It
also created a .xsd file where one of the columns type is set to date.
My program is being used in many different countries, and many
different local settings. Some time ago a user from Turkey contacted
me saying my program crashed on his system with the following error
code:
System.ArgumentException:...
|
by: webmonkie |
last post by:
Hi, I am hoping someone can point me in the right direction as I have been trying to fix this issue for a good while now.
I am trying to take information out of a xml file using php and parsing it into a MYSQL database but when I do this it takes all the information correctly out of the xml file and puts it into the database apart from the date. The date for each article is the same as the first one for example
Article 1 (Created...
|
by: Phillip B Oldham |
last post by:
Hi. I'm stretching my boundaries in programming with a little python
shell-script which is going to loop through a list of domain names,
grab the whois record, parse it, and put the results into a csv.
I've got the results coming back fine, but since I have *no*
experience with python I'm wondering what would be the preferred
"pythonic" way of parsing the whois string into a csv record.
Tips/thoughts/examples more than welcome!
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |