0

I am playing a bit with AWS via python and boto. I am trying to get modified date for keys from AWS bucket. After that I am parsing date to 'regular' date format and try to add every value to list.

Unfortunately, when I append values to list and try to print it's results I am getting datetime function instead of string with date. Code looks like this:

import boto
s3 = boto.connect_s3()
bucket = s3.get_bucket('mybucket')
keys = bucket.list
keys_latest_modified_date_list = []

for key in keys:
    key_latest_modified_date = boto.utils.parse_ts(key.last_modified)
    keys_latest_modified_date_list.append(key_latest_modified_date)
    print key_latest_modified_date
print keys_latest_modified_date_list

First print returns correct date:

2015-02-18 10:11:58

While second print gives me something like this:

[datetime.datetime(2015, 2, 18, 10, 11, 58)]

Does anyone know how to print values from list like in first print instead of this what I get from second one?

1 Answer 1

2

You are getting confused by the str() output of datetime objects and their representation:

>>> import datetime
>>> dt = datetime.datetime(2015, 2, 18, 10, 11, 58)
>>> dt
datetime.datetime(2015, 2, 18, 10, 11, 58)
>>> str(dt)
'2015-02-18 10:11:58'
>>> repr(dt)
'datetime.datetime(2015, 2, 18, 10, 11, 58)'

print calls str on the objects you print, but lists and other containers always show their contents with repr().

You'll have to use some string formatting to process the list:

print ', '.join(map(str, keys_latest_modified_date_list))

would print a comma-separated list of datetime objects converted to strings:

>>> keys_latest_modified_date_list = [dt, dt]
>>> keys_latest_modified_date_list
[datetime.datetime(2015, 2, 18, 10, 11, 58), datetime.datetime(2015, 2, 18, 10, 11, 58)]
>>> ', '.join(map(str, keys_latest_modified_date_list))
'2015-02-18 10:11:58, 2015-02-18 10:11:58'
>>> print ', '.join(map(str, keys_latest_modified_date_list))
2015-02-18 10:11:58, 2015-02-18 10:11:58
Sign up to request clarification or add additional context in comments.

4 Comments

Looks fine. But is there a possibility to add 'regular' string to list? I would like to make more operation on a list (ie. sorting) and 'regular' string will be more useful. Printing content of the list is not my main goal. Thanks in advance.
@Piotr: no, there is not. List objects are not meant to be used as end-user presentation. If you need a specific format, you need to explicitly format it yourself.
@Piotr: you'd convert the objects to strings when you need to present the data. If you are sorting or doing other operations with the datetime objects, then keep those objects and don't convert them to strings.
@MartijnPieters Would you please give a word here, stackoverflow.com/questions/28665037/… I would be thankful

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.