0
cur.execute('Select ts from testtable where nd_a>%s and nd_b>%s and nd_c>%s',(medA,medB,medC))
result_ts=cur.fetchall()
print (result_ts)

when i get the output this is look like this:

((datetime.datetime(2020, 1, 1, 1, 15, 24),), (datetime.datetime(2020, 1, 1, 1, 15, 38),), (datetime.datetime(2020, 1, 1, 1, 16, 30),), (datetime.datetime(2020, 1, 1, 1, 16, 37),), (datetime.datetime(2020, 1, 1, 1, 17, 8),), (datetime.datetime(2020, 1, 1, 1, 17, 14),))

When I need the output date in this format: YYYY-MM-DD HH:MM:SS

How I change it to this format view?

1
  • 1
    You need to print the values: print (result_ts[0]) Commented Jan 8, 2020 at 18:01

2 Answers 2

1

You could do something like this:

cur.execute('Select ts from testtable where nd_a>%s and nd_b>%s and nd_c>%s',(medA,medB,medC))
result_ts=cur.fetchall()

for result in result_ts:
    print(result[0])

This will print:

2020-01-01 01:15:24
2020-01-01 01:15:38
2020-01-01 01:16:30
2020-01-01 01:16:37
2020-01-01 01:17:08
2020-01-01 01:17:14
Sign up to request clarification or add additional context in comments.

Comments

0

If you'd like a string timestamp, you should use the strftime method.

cur.execute('Select ts from testtable where nd_a>%s and nd_b>%s and nd_c>%s',(medA,medB,medC))
result_ts=cur.fetchall()
timestamps = []
for r in results_ts:
    timestamps.append(r[0].strftime('%Y-%m-%d %H:%M:%s'))

This will give you a list of results translated into timestamp strings :)

2 Comments

%Y-%m-%d %H:%M:%s is the default format. There's no point using of using strftime!
Depends on what they want to do with it - if they just want to print it, print() is good, but if they hope to use it for something then strftime might be helpful :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.