0

I have a tuple with a string of times and activities. So, it would be similar to this:

('08:01: Woke Up\n08:05: Took Shower\n08:20: Ate Breakfast\n08:45: Left for work', )

I need to be able to take each activity and display individually like so:

8:01 Woke Up
8:05 Took Shower
8:20 Ate Breakfast
8:45 Left for work

Can anyone give me a suggestion for the best/easiest way to do this in python? Thank you.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
avaholic
  • 120
  • 2
  • 11

4 Answers4

9

You already have newlines in the element, so just print that:

>>> tup = ('08:01: Woke Up\n08:05: Took Shower\n08:20: Ate Breakfast\n08:45: Left for work',)
>>> print tup[0]
08:01: Woke Up
08:05: Took Shower
08:20: Ate Breakfast
08:45: Left for work

You can split out the string into separate elements by using str.splitlines():

>>> tup[0].splitlines()
['08:01: Woke Up', '08:05: Took Shower', '08:20: Ate Breakfast', '08:45: Left for work']
>>> for line in tup[0].splitlines():
...     print line
... 
08:01: Woke Up
08:05: Took Shower
08:20: Ate Breakfast
08:45: Left for work

Should you require the times and activities as separate strings, split each line on the string ': ':

>>> for line in tup[0].splitlines():
...     time, activity = line.split(': ', 1)
...     print time, activity
... 
08:01 Woke Up
08:05 Took Shower
08:20 Ate Breakfast
08:45 Left for work
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Great answer. And if you want to split each line up into a time and an activity, you can do `line.split(None, 1)` or `line.partition(' ')`. – abarnert Aug 27 '13 at 21:33
  • @abarnert: splitting on `': '`, actually; now my output matches the question sample. – Martijn Pieters Aug 27 '13 at 21:40
  • Very detailed and spot on answer. Thank you, and thanks to the other responses as well. – avaholic Aug 27 '13 at 21:48
  • @MartijnPieters: Yeah, that works, but I'm not sure it's more readable; you have to think about the fact that the first colon doesn't count because it doesn't have a space after it… – abarnert Aug 27 '13 at 22:02
0
>>> t = ('08:01: Woke Up\n08:05: Took Shower\n08:20: Ate Breakfast\n08:45: Left for work', )
>>> for elem in t:
...     print elem.split('\n')
...
['08:01: Woke Up', '08:05: Took Shower', '08:20: Ate Breakfast', '08:45: Left fo
r work']
>>> for elem in t:
...     for activity in elem.split('\n'):
...             print activity
...
08:01: Woke Up
08:05: Took Shower
08:20: Ate Breakfast
08:45: Left for work
>>> for elem in t:
...     print elem
...
08:01: Woke Up
08:05: Took Shower
08:20: Ate Breakfast
08:45: Left for work
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
0

You don't seem to have a tuple, you have a string which, if you print, will give you precisely what you want.

Supposing you had a list of tuples (which I assume is what you meant) you should do something like this:

input = [("8:01", "Woke Up"), ("8:05", "Took shower"), ("8:20", "Ate Breakfast")]
for time, description in input:
  print time, description
nickie
  • 5,608
  • 2
  • 23
  • 37
0

Have you ever thought of dictionaries? You could store the time information as key and the activity as value, as such:

dictionary = {}
dictionary['8:01'] = 'Woke Up'
dictionary['8:05'] = 'Took Shower'
dictionary['8:20'] = 'Ate Breakfast'
dictionary['8:45'] = 'Left for work'

Then to print in the format you could simply do:

for k in dictionary:
    print k + ' ' + dictionary[k]
darmat
  • 698
  • 4
  • 10