2017-01-15 45 views
-3

我有下面這段代碼來獲取名稱和發佈時間爲每個實例:獲取在Python變量多串

for instance in instances: 

    instance_name = instance.name 
    launch_time = instance.launch_time 

至於結果,我想與列表像一個變量:

instance_name: launch_time 
instance_name: launch_time 
... 

Name: server1; Launch time: 2 days, 7:33:46.319073 
Name: server2: Launch time: 4 days, 6:33:46.319073 
... 

然後在另一個多行字符串使用電子郵件通知。 我已經做到了:

result = [] 
result.append({'name':instance_name, 'launch_uptime':uptime }) 

,但我不喜歡這些報價,我希望有一個好的格式的文本

+0

所以,你要一個字符串列表?或字典?別的什麼? – Sayse

+1

沒有引號的格式化文本,並獲得每個名稱和發佈時間新行 – BigBoss

+0

你是什麼意思? – Sayse

回答

1

你可以這樣做:

multi_line = '' 

for instance in instances: 
    instance_name = instance.name 
    launch_time = instance.launch_time 

    multi_line += 'Name: ' + instance_name +'; Launch time: ' + launch_time + '\n' 

print(multi_line) 
+1

那正是我需要的!非常感謝! – BigBoss