2017-07-27 78 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