2

I would like to ask how to exchange dates from loop in to an array in python? I need an array of irregular, random dates with hours. So, I prepared a solution:

import datetime
import radar
r2 =()
for a in range(1,10):
       r2 = r2+(radar.random_datetime(start='1985-05-01', stop='1985-05-04'),)
r3 = list(r2)
print(r3)

As the result I get a list like:

[datetime.datetime(1985, 5, 3, 17, 59, 13), datetime.datetime(1985, 5, 2, 15, 58, 30), datetime.datetime(1985, 5, 2, 9, 46, 35), datetime.datetime(1985, 5, 3, 10, 5, 45), datetime.datetime(1985, 5, 2, 4, 34, 43), datetime.datetime(1985, 5, 3, 9, 52, 51), datetime.datetime(1985, 5, 2, 22, 7, 17), datetime.datetime(1985, 5, 1, 15, 28, 14), datetime.datetime(1985, 5, 3, 13, 33, 56)]

But I need strings in the list like:

list2 = ['1985-05-02 08:48:46','1985-05-02 10:47:56','1985-05-03 22:07:11', '1985-05-03 22:07:11','1985-05-01 03:23:43']
4
  • Convert it to a string like: r2 = r2+(str(radar.random_datetime(start='1985-05-01', stop='1985-05-04')),) Commented Jan 28, 2019 at 5:39
  • Its works! Thanks a lot, how can I award you? Commented Jan 28, 2019 at 5:44
  • You can accept the answer. And if you hang for a while and get 15 reputation, you can start upvoting answers. Commented Jan 28, 2019 at 5:48
  • I will do it, Thanks :-). I will send award for this everybody after obtain 15 reputation. I promise! Commented Jan 28, 2019 at 6:01

2 Answers 2

3

You can convert the datetime to a string with str() like:

Code:

str(radar.random_datetime(start='1985-05-01', stop='1985-05-04'))

Test Code:

import radar

r2 = ()
for a in range(1, 10):
    r2 = r2 + (str(
        radar.random_datetime(start='1985-05-01', stop='1985-05-04')),)
r3 = list(r2)
print(r3)

Results:

['1985-05-01 21:06:29', '1985-05-01 04:43:11', '1985-05-02 13:51:03', 
 '1985-05-03 03:20:44', '1985-05-03 19:59:14', '1985-05-02 21:50:34', 
 '1985-05-01 04:13:50', '1985-05-03 23:28:36', '1985-05-02 15:56:23']
Sign up to request clarification or add additional context in comments.

Comments

2

Use strftime to convert the date generated by radar before adding it to the list. e.g.

import datetime
import radar
r2 =()
for a in range(1,10):
 t=datetime.datetime(radar.random_datetime(start='1985-05-01', stop='1985-05-04'))
 r2 = r2+(t.strftime('%Y-%m-%d %H:%M:%S'),)
r3 = list(r2)
print(r3)

3 Comments

strftime() is great solution if you need a non-standard format. The date format the OP desires is the standard format, so a simple str() can do that. Also it is a good idea to format your answers so that the most important information is presented first. This allows subsequent readers to find the most important parts of your answer quickly. Look at how I presented my answer for one way to do this. Cheers.
Infosys from Bubaneswar Orisa? I heve been there from Xavier MBA.
@WojciechMoszczyński I'm working in other Infosys Development Center, it is Mohali, India.

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.