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? -
from datatime import datetime
-
TmpArr = []
-
reader = open("c:/test.csv",'r')
-
for line in reader:
-
TmpArr = line.split(',')
-
#now TmpArr[1] contains the datatime data
-
#i've tried this
-
dt = datetime(TmpArr[1])
-
#but get a needs an integer error
-
#i've tried this too
-
dt = datetime.strtime(TmpArr[1],"%Y-%M-%D %h:%m:%s")
-
# then i get a needs datetime object
-
lost and would greatly appreciate anyones help with this... Using Python 2.6.3
Thanks ahead of time.
Cheers,
Eric
You have to go to the time module to get what you need. - import datetime, time
-
-
s = "2010-01-15 23:15:30"
-
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. - import datetime, time
-
-
s = "2010-01-15 23:15:30"
-
print repr(datetime.datetime(*time.strptime(s, "%Y-%m-%d %H:%M:%S")[:6]))
Output:
>>> datetime.datetime(2010, 1, 15, 23, 15, 30)
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!!!
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: - >>> time.strptime(s, "%Y-%m-%d %H:%M:%S")
-
(2010, 1, 15, 23, 15, 30, 4, 15, -1)
-
>>> time.strptime(s, "%Y-%m-%d %H:%M:%S")[:6]
-
(2010, 1, 15, 23, 15, 30)
-
>>> datetime.datetime(time.strptime(s, "%Y-%m-%d %H:%M:%S")[:6])
-
Traceback (most recent call last):
-
File "<interactive input>", line 1, in ?
-
TypeError: function takes at least 3 arguments (1 given)
-
>>> datetime.datetime(*time.strptime(s, "%Y-%m-%d %H:%M:%S")[:6])
-
datetime.datetime(2010, 1, 15, 23, 15, 30)
-
>>>
Thanks, that does make more sense now!
Sign in to post your reply or Sign up for a free account.
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 $...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
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...
| |