2017-07-27 47 views
0
 "allytips": [ 
      "Jax can Leap Strike to friendly units, including wards. You can use them to plan your escape.", 
      "Jax benefits greatly from items that have both Ability Power and Attack Damage such as Guinsoo's Rageblade and Hextech Gunblade." 
     ], 

我試圖在allytips作爲單獨的對象返回的上述數據,以使得我可以能夠格式化/更吸引人的方式風格化如在每個端\ n字符串我嘗試了for語句,因爲我認爲它會使它更容易訪問,但它沒有,它只打印一個通常是最後一個的對象。我可以做,如果語句分配給他們一個變量,然後使一個乾淨的打印語句,但我不知道我會怎麼做.format(),因爲如果它不存在它會給我一個錯誤,我會有空白行的很多,如果我做\ N {4} \ N {5},他們不存在返回在JSON單獨對象

try: 
      for tips in self.j['data'][champEntry]['allytips']: 
       self.allyTips0 = tips 
      print(tips) 
      allyRaw = self.j['data'][champEntry]['allytips'] 
      print(allyRaw) 

這就是它的返回,其中第一行我怎麼提到它只是在打印一個對象時間往往不是最後的

Remember that nearby enemies can see which wall you're in. 
["Look at the line-up of both your team and the enemy's team when picking your form.", "Remember that nearby enemies can 
see which wall you're in."] 

我試圖讓它看起來像這樣

Look at the line-up of both your team and the enemy's team when picking your form. 
Remember that nearby enemies can see which wall you're in. 

回答

0

試圖加入像這樣

環路
for i in range(0,len(self.j['data'][champEntry]['allytips'])): 
     print self.j['data'][champEntry]['allytips'][i],"\n" 
0

嘗試split()方法,該方法將按創建數組的分隔符分割。然後按索引選擇。

allyRaw1 = self.j['data'][champEntry]['allytips'].split(',')[0] 

allyRaw2 = self.j['data'][champEntry]['allytips'].split(',')[1] 
+0

會有在分裂點沒有我可以只是做allyRaw1 = self.j [ '數據'] [cham​​pEntry] [ 'allytips'] [0 ]但我不想創建一堆if語句來分配變量(如果它存在,因爲可能有8或10個以上的字符串),那麼必須執行print(\ n {0} \ n {1} \格式(allyRaw,allyRaw1,allyRaw2)會變得凌亂,如果allyRaw2不存在 – oso9817