Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #11354

Re: How do I convert String into Date object

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'sys': 0.05; 'attribute': 0.07; 'subject:object': 0.07; 'python': 0.08; '__name__': 0.09; 'csv': 0.09; 'either.': 0.09; 'filename': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'skip:[ 20': 0.12; 'def': 0.15; '"__main__":': 0.16; 'comparisons,': 0.16; 'delimited': 0.16; 'etc?': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'row': 0.16; 'wrote:': 0.16; 'thanks,': 0.18; '>>>': 0.18; 'convert': 0.19; '(most': 0.21; 'structure': 0.23; 'pm,': 0.24; 'aug': 0.24; 'object,': 0.24; 'traceback': 0.24; 'string': 0.26; "i'm": 0.27; 'correct': 0.28; 'import': 0.28; '27,': 0.29; 'yield': 0.29; 'looks': 0.29; 'print': 0.29; 'module': 0.30; 'construct': 0.30; 'from:addr:web.de': 0.30; "skip:' 10": 0.30; 'time:': 0.32; 'there': 0.33; 'to:addr:python-list': 0.33; 'last):': 0.34; 'header:X-Complaints-To:1': 0.35; 'object': 0.35; 'subject:How': 0.35; 'file': 0.36; 'but': 0.37; 'received:org': 0.38; 'subject:: ': 0.39; 'header:Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'skip:d 20': 0.39; 'more': 0.60; 'discovered': 0.68; 'records': 0.72; 'date?': 0.84; 'datetime': 0.84; 'spaces.': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: How do I convert String into Date object
Date Sat, 13 Aug 2011 22:29:35 +0200
Organization None
References <83822ecb-3643-42c6-a2bf-0187c07d351d@a10g2000yqn.googlegroups.com> <6f4283c7-794c-41f1-93fd-6d9be73894c2@w11g2000vbp.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p50849919.dip.t-dialin.net
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2256.1313267341.1164.python-list@python.org> (permalink)
Lines 68
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1313267341 news.xs4all.nl 23930 [2001:888:2000:d::a6]:47103
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:11354

Show key headers only | View raw


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']

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web