473,383 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

dealing with datetime while parsing a csv

58
Hello all!
I am parsing a csv file and one of the fields is a date time field that looks something like this
2010-01-15 23:15:30
year-month-day hour24:minute:second

as i loop through this csv i am going to need to do some time arithmetic. my question is, how do i turn that field from a string in my list to a datetime object?
Expand|Select|Wrap|Line Numbers
  1. from datatime import datetime
  2. TmpArr = []
  3. reader = open("c:/test.csv",'r')
  4. for line in reader:
  5.     TmpArr = line.split(',')
  6.     #now TmpArr[1] contains the datatime data
  7.     #i've tried this
  8.     dt = datetime(TmpArr[1])
  9.     #but get a needs an integer error
  10.     #i've tried this too
  11.     dt = datetime.strtime(TmpArr[1],"%Y-%M-%D %h:%m:%s")
  12.     # then i get a needs datetime object
  13.  
lost and would greatly appreciate anyones help with this... Using Python 2.6.3
Thanks ahead of time.
Cheers,
Eric
Sep 10 '10 #1

✓ answered by bvdet

You have to go to the time module to get what you need.
Expand|Select|Wrap|Line Numbers
  1. import datetime, time
  2.  
  3. s = "2010-01-15 23:15:30"
  4. print repr(datetime.datetime(*time.strptime(s, "%Y-%m-%d %H:%M:%S")[:6]))
Output:
>>> datetime.datetime(2010, 1, 15, 23, 15, 30)

4 10632
bvdet
2,851 Expert Mod 2GB
You have to go to the time module to get what you need.
Expand|Select|Wrap|Line Numbers
  1. import datetime, time
  2.  
  3. s = "2010-01-15 23:15:30"
  4. print repr(datetime.datetime(*time.strptime(s, "%Y-%m-%d %H:%M:%S")[:6]))
Output:
>>> datetime.datetime(2010, 1, 15, 23, 15, 30)
Sep 10 '10 #2
erbrose
58
Thanks so much...
now seeing as i don't really see something like this in the documentation, could you explain this line a bit more?
datetime.datetime(*time.strptime(s, "%Y-%m-%d %H:%M:%S")[:6])

I understand calling datetime.datetime, but why the * before time? and also what exactly is the [:6] pertaining too
Thanks again for your help!!!
Sep 10 '10 #3
bvdet
2,851 Expert Mod 2GB
time.strptime returns a struct_time object with more attributes than datetime.datetime can handle, therefore the slice ([:6]). The asterisk expands the remaining tuple into the individual tuple elements. Example:
Expand|Select|Wrap|Line Numbers
  1. >>> time.strptime(s, "%Y-%m-%d %H:%M:%S")
  2. (2010, 1, 15, 23, 15, 30, 4, 15, -1)
  3. >>> time.strptime(s, "%Y-%m-%d %H:%M:%S")[:6]
  4. (2010, 1, 15, 23, 15, 30)
  5. >>> datetime.datetime(time.strptime(s, "%Y-%m-%d %H:%M:%S")[:6])
  6. Traceback (most recent call last):
  7.   File "<interactive input>", line 1, in ?
  8. TypeError: function takes at least 3 arguments (1 given)
  9. >>> datetime.datetime(*time.strptime(s, "%Y-%m-%d %H:%M:%S")[:6])
  10. datetime.datetime(2010, 1, 15, 23, 15, 30)
  11. >>> 
Sep 10 '10 #4
erbrose
58
Thanks, that does make more sense now!
Sep 10 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
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 $...
9
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...
2
by: Simon | last post by:
Hi all If I have a method for inserting data to database and it take a parameter dateTime in String. Since C# does not have a function to test date like IsDate in VB.Net. Would it be a good idea...
6
by: Ante Perkovic | last post by:
Hi, How to declare datetime object and set it to my birthday, first or last day of this month or any other date. I can't find any examples in VS.NET help! BTW, what is the difference...
50
by: z. f. | last post by:
HI, i have string in format dd/mm/yyyyy hh:mm:ss and giving this as an input to DateTime.Parse gives a string was not recognized as a valid date time format string error. how do i make the parse...
18
by: Atara | last post by:
In my apllication I use the following code: '-- My Code: Public Shared Function strDate2Date(ByVal strDate As String) As System.DateTime Dim isOk As Boolean = False If (strDate Is Nothing)...
9
by: Phil B | last post by:
I am having a problem with a datetime from a web services provider The provider is sending the following SOAP response <?xml version="1.0" encoding="utf-8"?> <soap:Envelope...
1
by: John Dann | last post by:
I have some dates in a file that I need to read as strings in the format eg '8.Aug.06' and then convert to a .Net date type. The date is also available as day of the year, ie an integer. Does...
4
by: Michael Meckelein | last post by:
Hello, Wondering, if C# (framework 2.0) does not support parsing DateTime timezones in three letter acronyms. I would like to parse date strings like "2005 Nov 01 11:58:47.490 CST -6:00" but...
7
by: Daniel Fetchinson | last post by:
Many times a more user friendly date format is convenient than the pure date and time. For example for a date that is yesterday I would like to see "yesterday" instead of the date itself. And for...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.