您已經有換行符的元素,所以只打印:
>>> 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
您可以通過使用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
你應該要求時間和活動作爲單獨的字符串,分割字符串上的每一行': '
:
>>> 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
偉大的答案。如果你想將每一行分成時間和活動,你可以使用'line.split(None,1)'或'line.partition('')'。 – abarnert
@abarnert:實際上分割爲'':'';現在我的輸出與問題示例相匹配。 –
非常詳細和現場答案。謝謝,並感謝其他回覆。 – avaholic