Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #11350 > unrolled thread
| Started by | MrPink <tdsimpson@gmail.com> |
|---|---|
| First post | 2011-08-13 12:14 -0700 |
| Last post | 2011-08-13 22:44 -0700 |
| Articles | 9 — 6 participants |
Back to article view | Back to comp.lang.python
How do I convert String into Date object MrPink <tdsimpson@gmail.com> - 2011-08-13 12:14 -0700
Re: How do I convert String into Date object MrPink <tdsimpson@gmail.com> - 2011-08-13 12:26 -0700
Re: How do I convert String into Date object Rafael Durán Castañeda <rafadurancastaneda@gmail.com> - 2011-08-13 22:11 +0200
Re: How do I convert String into Date object MrPink <tdsimpson@gmail.com> - 2011-08-13 13:25 -0700
Re: How do I convert String into Date object Peter Otten <__peter__@web.de> - 2011-08-13 22:29 +0200
Re: How do I convert String into Date object Sibylle Koczian <nulla.epistola@web.de> - 2011-08-13 22:05 +0200
Re: How do I convert String into Date object Chris Rebert <clp2@rebertia.com> - 2011-08-13 13:09 -0700
Re: How do I convert String into Date object Roy Smith <roy@panix.com> - 2011-08-13 20:49 -0400
Re: How do I convert String into Date object MrPink <tdsimpson@gmail.com> - 2011-08-13 22:44 -0700
| From | MrPink <tdsimpson@gmail.com> |
|---|---|
| Date | 2011-08-13 12:14 -0700 |
| Subject | How do I convert String into Date object |
| Message-ID | <83822ecb-3643-42c6-a2bf-0187c07d351d@a10g2000yqn.googlegroups.com> |
Is this the correct way to convert a String into a Date?
I only have dates and no time.
import time, datetime
oDate = time.strptime('07/27/2011', '%m/%d/%Y')
print oDate
Thanks,
[toc] | [next] | [standalone]
| From | MrPink <tdsimpson@gmail.com> |
|---|---|
| Date | 2011-08-13 12:26 -0700 |
| Message-ID | <6f4283c7-794c-41f1-93fd-6d9be73894c2@w11g2000vbp.googlegroups.com> |
| In reply to | #11350 |
I have file of records delimited by spaces.
I need to import the date string and convert them into date datatypes.
'07/27/2011' 'Event 1 Description'
'07/28/2011' 'Event 2 Description'
'07/29/2011' 'Event 3 Description'
I just discovered that my oDate is not an object, but a structure and
not a date datatype.
I'm stumped. Is there a way to convert a string into a date datatype
for comparisons, equality, etc?
Thanks,
On Aug 13, 3:14 pm, MrPink <tdsimp...@gmail.com> wrote:
> Is this the correct way to convert a String into a Date?
> I only have dates and no time.
>
> import time, datetime
>
> oDate = time.strptime('07/27/2011', '%m/%d/%Y')
> print oDate
>
> Thanks,
[toc] | [prev] | [next] | [standalone]
| From | Rafael Durán Castañeda <rafadurancastaneda@gmail.com> |
|---|---|
| Date | 2011-08-13 22:11 +0200 |
| Message-ID | <mailman.2255.1313266271.1164.python-list@python.org> |
| In reply to | #11351 |
You can use datetime objects:
>>> dt1 = datetime.datetime.strptime('07/27/2011',"%m/%d/%Y")
>>> dt2 =datetime.datetime.strptime('07/28/2011',"%m/%d/%Y")
>>> dt1 == dt2
False
>>> dt1 > dt2
False
>>> dt1 < dt2
True
>>> dt1 - dt2
datetime.timedelta(-1)
On 13/08/11 21:26, MrPink wrote:
> I have file of records delimited by spaces.
> I need to import the date string and convert them into date datatypes.
>
> '07/27/2011' 'Event 1 Description'
> '07/28/2011' 'Event 2 Description'
> '07/29/2011' 'Event 3 Description'
>
> I just discovered that my oDate is not an object, but a structure and
> not a date datatype.
> I'm stumped. Is there a way to convert a string into a date datatype
> for comparisons, equality, etc?
>
> Thanks,
>
> On Aug 13, 3:14 pm, MrPink<tdsimp...@gmail.com> wrote:
>> Is this the correct way to convert a String into a Date?
>> I only have dates and no time.
>>
>> import time, datetime
>>
>> oDate = time.strptime('07/27/2011', '%m/%d/%Y')
>> print oDate
>>
>> Thanks,
[toc] | [prev] | [next] | [standalone]
| From | MrPink <tdsimpson@gmail.com> |
|---|---|
| Date | 2011-08-13 13:25 -0700 |
| Message-ID | <f2d78383-272a-419a-993f-904ff687c0c3@j1g2000yqn.googlegroups.com> |
| In reply to | #11353 |
BTW, here is the Python version I'm using.
Will this make a difference with the solutions you guys provided?
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Also, what editor do you guys use?
There are so many to chose from.
On Aug 13, 4:11 pm, Rafael Durán Castañeda
<rafadurancastan...@gmail.com> wrote:
> You can use datetime objects:
>
> >>> dt1 = datetime.datetime.strptime('07/27/2011',"%m/%d/%Y")
> >>> dt2 =datetime.datetime.strptime('07/28/2011',"%m/%d/%Y")
> >>> dt1 == dt2
> False
> >>> dt1 > dt2
> False
> >>> dt1 < dt2
> True
> >>> dt1 - dt2
> datetime.timedelta(-1)
>
> On 13/08/11 21:26, MrPink wrote:
>
>
>
>
>
>
>
> > I have file of records delimited by spaces.
> > I need to import the date string and convert them into date datatypes.
>
> > '07/27/2011' 'Event 1 Description'
> > '07/28/2011' 'Event 2 Description'
> > '07/29/2011' 'Event 3 Description'
>
> > I just discovered that my oDate is not an object, but a structure and
> > not a date datatype.
> > I'm stumped. Is there a way to convert a string into a date datatype
> > for comparisons, equality, etc?
>
> > Thanks,
>
> > On Aug 13, 3:14 pm, MrPink<tdsimp...@gmail.com> wrote:
> >> Is this the correct way to convert a String into a Date?
> >> I only have dates and no time.
>
> >> import time, datetime
>
> >> oDate = time.strptime('07/27/2011', '%m/%d/%Y')
> >> print oDate
>
> >> Thanks,
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2011-08-13 22:29 +0200 |
| Message-ID | <mailman.2256.1313267341.1164.python-list@python.org> |
| In reply to | #11351 |
MrPink wrote:
> I have file of records delimited by spaces.
> I need to import the date string and convert them into date datatypes.
>
> '07/27/2011' 'Event 1 Description'
> '07/28/2011' 'Event 2 Description'
> '07/29/2011' 'Event 3 Description'
>
> I just discovered that my oDate is not an object, but a structure and
> not a date datatype.
> I'm stumped. Is there a way to convert a string into a date datatype
> for comparisons, equality, etc?
>
> Thanks,
>
> On Aug 13, 3:14 pm, MrPink <tdsimp...@gmail.com> wrote:
>> Is this the correct way to convert a String into a Date?
>> I only have dates and no time.
>>
>> import time, datetime
That looks like a fifty-percent chance to try the "wrong" module ;)
>> oDate = time.strptime('07/27/2011', '%m/%d/%Y')
>> print oDate
>>> import datetime as dt
>>> d = dt.date.strptime("07/27/2011", "%m/%d/%Y")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'datetime.date' has no attribute 'strptime'
So you cannot construct a date from a date string either.
One more time:
>>> d = dt.datetime.strptime("07/27/2011", "%m/%d/%Y")
>>> d
datetime.datetime(2011, 7, 27, 0, 0)
>>> d.date()
datetime.date(2011, 7, 27)
$ cat csv_dates.csv
'07/27/2011' 'Event 1 Description'
'07/28/2011' 'Event 2 Description'
'07/29/2011' 'Event 3 Description'
$ cat csv_dates.py
import datetime
import csv
def rows(instream):
for row in csv.reader(instream, delimiter=" ", quotechar="'"):
row[0] = datetime.datetime.strptime(row[0], '%m/%d/%Y').date()
yield row
if __name__ == "__main__":
import sys
filename = sys.argv[1]
with open(filename, "rb") as instream:
for row in rows(instream):
print row
$ python csv_dates.py csv_dates.csv
[datetime.date(2011, 7, 27), 'Event 1 Description']
[datetime.date(2011, 7, 28), 'Event 2 Description']
[datetime.date(2011, 7, 29), 'Event 3 Description']
[toc] | [prev] | [next] | [standalone]
| From | Sibylle Koczian <nulla.epistola@web.de> |
|---|---|
| Date | 2011-08-13 22:05 +0200 |
| Message-ID | <mailman.2261.1313311210.1164.python-list@python.org> |
| In reply to | #11351 |
Am 13.08.2011 21:26, schrieb MrPink:
> I have file of records delimited by spaces.
> I need to import the date string and convert them into date datatypes.
>
> '07/27/2011' 'Event 1 Description'
> '07/28/2011' 'Event 2 Description'
> '07/29/2011' 'Event 3 Description'
>
> I just discovered that my oDate is not an object, but a structure and
> not a date datatype.
> I'm stumped. Is there a way to convert a string into a date datatype
> for comparisons, equality, etc?
>
There is. Only not in one step, if you want a date and not a datetime
instance:
>>> import datetime
>>> oDateTime = datetime.datetime.strptime('07/27/2011', '%m/%d/%Y')
>>> oDateTime
datetime.datetime(2011, 7, 27, 0, 0)
>>> oDate = oDateTime.date()
>>> oDate
datetime.date(2011, 7, 27)
>>>
HTH
Sibylle
[toc] | [prev] | [next] | [standalone]
| From | Chris Rebert <clp2@rebertia.com> |
|---|---|
| Date | 2011-08-13 13:09 -0700 |
| Message-ID | <mailman.2254.1313266161.1164.python-list@python.org> |
| In reply to | #11350 |
On Sat, Aug 13, 2011 at 12:14 PM, MrPink <tdsimpson@gmail.com> wrote:
> Is this the correct way to convert a String into a Date?
> I only have dates and no time.
>
> import time, datetime
>
> oDate = time.strptime('07/27/2011', '%m/%d/%Y')
> print oDate
from datetime import datetime
the_date = datetime.strptime('07/27/2011', '%m/%d/%Y').date()
Cheers,
Chris
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2011-08-13 20:49 -0400 |
| Message-ID | <roy-AD39E5.20490513082011@news.panix.com> |
| In reply to | #11350 |
In article <83822ecb-3643-42c6-a2bf-0187c07d351d@a10g2000yqn.googlegroups.com>, MrPink <tdsimpson@gmail.com> wrote: > Is this the correct way to convert a String into a Date? > I only have dates and no time. You have already received a number of good replies, but let me throw out one more idea. If you ever need to do something with dates and times which the standard datetime module can't handle, take a look at the excellent dateutil module by Gustavo Niemeyer (http://labix.org/python-dateutil).
[toc] | [prev] | [next] | [standalone]
| From | MrPink <tdsimpson@gmail.com> |
|---|---|
| Date | 2011-08-13 22:44 -0700 |
| Message-ID | <d4a594b9-754a-4c41-823b-6906af5f68b8@h14g2000yqd.googlegroups.com> |
| In reply to | #11350 |
I found this solution.
Python: Convert String Date to Date Object
http://slaptijack.com/programming/python-convert-string-date-to-date-object/
On Aug 13, 3:14 pm, MrPink <tdsimp...@gmail.com> wrote:
> Is this the correct way to convert a String into a Date?
> I only have dates and no time.
>
> import time, datetime
>
> oDate = time.strptime('07/27/2011', '%m/%d/%Y')
> print oDate
>
> Thanks,
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web