Convert string to datetime to epoch

kumarprd

I am using Python to query a MySQL table and getting one datetime in string format, which is stored in row[3]. I need to convert this string timestamp to epoch seconds.

import MySQLdb
import os
import datetime
try:
    db = MySQLdb.connect("localhost","root","","test" )
except MySQLdb.Error, e:
     print "Error %d: %s" % (e.args[0], e.args[1])
     sys.exit (1)

cursor = db.cursor()
cursor.execute ("SELECT * from main_tbl WHERE login_user_name='kumar'")
data = cursor.fetchall()

for row in data :
    print row[3]                                   ###printing 2014-09-26 12:24:23
    date = datetime.datetime.strptime(row[3], "%Y-%m-%d %H:%M:%S")
    print date

On execution it throws this error:

2014-09-26 12:24:23
Traceback (most recent call last):
  File "test1.py", line 22, in <module>
    date = datetime.datetime.strptime(row[3], "%Y-%m-%d %H:%M:%S")
TypeError: must be string, not datetime.datetime

What am I doing wrong?

I have also tried the following:

epoch_start = time.mktime(time.strptime(row[3], "%Y-%m-%d %H:%M:%S"));

But I get this error:

Traceback (most recent call last):
  File "test1.py", line 29, in <module>
    epoch_start = time.mktime(time.strptime(row[3], "%Y-%m-%d %H:%M:%S"));
  File "C:\Python27\lib\_strptime.py", line 467, in _strptime_time
    return _strptime(data_string, format)[0]
  File "C:\Python27\lib\_strptime.py", line 322, in _strptime
    found = format_regex.match(data_string)
TypeError: expected string or buffer
Jayaram
row[3] is already in datetime.datetime format.

From your question its sounds like you want convert to EPOCH so do something like:

import time
epochTime = time.mktime(row[3].timetuple())
print epochTime

Then check if the converted epoch is correct or not:

print time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(epochTime))
print row[3]

Verify last two statements have the same output.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

PHP: Convert epoch to MySQL DateTime format

分類Dev

Python - code to convert any epoch time to formatted datetime

分類Dev

How to convert DateTime to a String

分類Dev

Trying to convert epoch code field (string type) to timestamp

分類Dev

Failed to Convert UTC TIME string date to Epoch time in C#

分類Dev

convert python datetime with timezone to string

分類Dev

Convert string to datetime - python dataframe

分類Dev

Java Convert String into datetime Object

分類Dev

DateTime, the Epoch and DocumentDb

分類Dev

Datetime to epoch conversion

分類Dev

BigQuery: convert epoch to TIMESTAMP

分類Dev

PHP: Convert String to DATETIME with Consideration of am/pm

分類Dev

How to convert string data with variable format to datetime?

分類Dev

Ruby on Rails - Convert String to a DateTime action

分類Dev

Convert a specific format dateTime string to DateTime in C#

分類Dev

How to convert efficiently a dataframe column of string type into datetime in Python?

分類Dev

Convert DateTime String to Date-only when listing in comboBox

分類Dev

How do I convert the string "NULL" in a flat file to DATETIME or DATETIME2?

分類Dev

How to convert Excel date/time to epoch

分類Dev

Python convert date to datetime

分類Dev

convert datetime to float in R

分類Dev

Convert index to datetime in pandas

分類Dev

DateTime convert issue in php

分類Dev

SQL DateTime Format Convert

分類Dev

Best way to convert a string like May 2nd 2017 to datetime in SQL Server

分類Dev

How do I convert multiple `string` columns in my dataframe to datetime columns?

分類Dev

How do I convert a Calendar object to an ISO 8601 format DateTime string?

分類Dev

Convert Eastern Standard Time String to UTC DateTime using Portable Class Library

分類Dev

Symfony2 Form Builder PRE_SET_DATA Convert Datetime to String Date

Related 関連記事

  1. 1

    PHP: Convert epoch to MySQL DateTime format

  2. 2

    Python - code to convert any epoch time to formatted datetime

  3. 3

    How to convert DateTime to a String

  4. 4

    Trying to convert epoch code field (string type) to timestamp

  5. 5

    Failed to Convert UTC TIME string date to Epoch time in C#

  6. 6

    convert python datetime with timezone to string

  7. 7

    Convert string to datetime - python dataframe

  8. 8

    Java Convert String into datetime Object

  9. 9

    DateTime, the Epoch and DocumentDb

  10. 10

    Datetime to epoch conversion

  11. 11

    BigQuery: convert epoch to TIMESTAMP

  12. 12

    PHP: Convert String to DATETIME with Consideration of am/pm

  13. 13

    How to convert string data with variable format to datetime?

  14. 14

    Ruby on Rails - Convert String to a DateTime action

  15. 15

    Convert a specific format dateTime string to DateTime in C#

  16. 16

    How to convert efficiently a dataframe column of string type into datetime in Python?

  17. 17

    Convert DateTime String to Date-only when listing in comboBox

  18. 18

    How do I convert the string "NULL" in a flat file to DATETIME or DATETIME2?

  19. 19

    How to convert Excel date/time to epoch

  20. 20

    Python convert date to datetime

  21. 21

    convert datetime to float in R

  22. 22

    Convert index to datetime in pandas

  23. 23

    DateTime convert issue in php

  24. 24

    SQL DateTime Format Convert

  25. 25

    Best way to convert a string like May 2nd 2017 to datetime in SQL Server

  26. 26

    How do I convert multiple `string` columns in my dataframe to datetime columns?

  27. 27

    How do I convert a Calendar object to an ISO 8601 format DateTime string?

  28. 28

    Convert Eastern Standard Time String to UTC DateTime using Portable Class Library

  29. 29

    Symfony2 Form Builder PRE_SET_DATA Convert Datetime to String Date

ホットタグ

アーカイブ