user2183592 user2183592 - 2 months ago 8
Python Question

Iterate over Python list and convert Python date to human readable string

I have a python list that stores IP addresses, hostnames and dates.

I need to iterate over this list and convert the python dates to human readable dates.

How can I iterate over the list and convert the dates to human readable strings?

List:

{'_id': '192.168.1.5', '192.168.1.5', 'u_updated_timestamp': datetime.datetime(2016, 9, 1, 20, 27, 38, 364000), 'u_hostname': 'test.example.com'}, {'_id': '192.168.1.3', 'u_ipv4': '192.168.1.3', 'u_updated_timestamp': datetime.datetime(2016, 9, 2, 9, 40, 5, 347000), 'u_hostname': test.test.com}, {'_id': '192.168.1.8', 'u_ipv4': '192.168.1.8', 'u_updated_timestamp': datetime.datetime(2016, 9, 2, 13, 13, 5, 403000), 'u_hostname': hosttest.example.com}

Answer

Simply iterate by the list and modify / add datetime to human-readable:

for row_id, row in enumerate(your_list):
    your_list[row_id]['human_readable_date'] = row['u_updated_timestamp'].strftime("specify-your-format")

You can find datetime formats here.