我正在执行的命令将吐出随机的饮料。问题是,某些饮料比其他饮料具有更多的成分和测量值,这会使某些键返回为null或None。我正在使用fix_data
将Nones变成空白,但这在我的嵌入中造成了巨大的差距。有没有更简单的方法来显示嵌入中的数据,还是更简单的方法来清除JSON中的None值?
def fix_data(data):
if type(data) is list:
for i, e in enumerate(data):
if e is None:
data[i] = ''
else:
fix_data(e)
if message.content.lower().startswith("!drink"):
url = 'https://thecocktaildb.com/api/json/v1/1/random.php'
res = requests.get(url)
data = res.json()
fix_data(data)
drinkname = data['drinks'][0]['strDrink']
drinkimage = data['drinks'][0]['strDrinkThumb']
instructions = data['drinks'][0]['strInstructions']
ingredient1 = data['drinks'][0]['strIngredient1']
ingredient2 = data['drinks'][0]['strIngredient2']
ingredient3 = data['drinks'][0]['strIngredient3']
ingredient4 = data['drinks'][0]['strIngredient4']
ingredient5 = data['drinks'][0]['strIngredient5']
ingredient6 = data['drinks'][0]['strIngredient6']
ingredient7 = data['drinks'][0]['strIngredient7']
ingredient8 = data['drinks'][0]['strIngredient8']
ingredient9 = data['drinks'][0]['strIngredient9']
ingredient10 = data['drinks'][0]['strIngredient10']
ingredient11 = data['drinks'][0]['strIngredient11']
ingredient12 = data['drinks'][0]['strIngredient12']
ingredient13 = data['drinks'][0]['strIngredient13']
ingredient14 = data['drinks'][0]['strIngredient14']
ingredient15 = data['drinks'][0]['strIngredient15']
measure1 = data['drinks'][0]['strMeasure1']
measure2 = data['drinks'][0]['strMeasure2']
measure3 = data['drinks'][0]['strMeasure3']
measure4 = data['drinks'][0]['strMeasure4']
measure5 = data['drinks'][0]['strMeasure5']
measure6 = data['drinks'][0]['strMeasure6']
measure7 = data['drinks'][0]['strMeasure7']
measure8 = data['drinks'][0]['strMeasure8']
measure9 = data['drinks'][0]['strMeasure9']
measure10 = data['drinks'][0]['strMeasure10']
measure11 = data['drinks'][0]['strMeasure11']
measure12 = data['drinks'][0]['strMeasure12']
measure13 = data['drinks'][0]['strMeasure13']
measure14 = data['drinks'][0]['strMeasure14']
measure15 = data['drinks'][0]['strMeasure15']
msg = '**Drink**: \n{}\n\n**Ingredients**: \n{} {}\n{} {}\n{} {}\n{} {}\n{} {}\n{} {}\n{} {}\n{} {}\n{} {}\n{} {}\n{} {}\n{} {}\n{} {}\n{} {}\n{} {}\n\n**Instructions**: \n{}'.format(drinkname,measure1, ingredient1, measure2, ingredient2, measure3, ingredient3, measure4, ingredient4, measure5, ingredient5, measure6, ingredient6, measure7, ingredient7, measure8, ingredient8, measure9, ingredient9, measure10, ingredient10, measure11, ingredient11, measure12, ingredient12, measure13,ingredient13, measure14, ingredient14 , measure15, ingredient15, instructions)
embed = discord.Embed(title="**? DRINK BOT ?**", description=msg, color=0x9554af)
embed.set_author(name="DRINK BOT", icon_url="https://i.imgur.com/RI2iiLd.png")
embed.set_thumbnail(url='{}'.format(drinkimage))
embed.set_footer(text="DRINK BOT", icon_url="https://i.imgur.com/RI2iiLd.png")
await client.send_message(message.channel, embed=embed)
答案 0 :(得分:1)
使用for循环和if语句检查每种成分是否均无。
import requests
url = 'https://thecocktaildb.com/api/json/v1/1/random.php'
res = requests.get(url)
data = res.json()
drinks = data['drinks'][0]
ingredients_list = ""
for i in range(1, 16):
if drinks[f"strIngredient{i}"]:
measure = drinks[f"strMeasure{i}"]
ingredient = drinks[f"strIngredient{i}"]
ingredients_list+=f"{measure if measure else ''} {ingredient}\n"
drinkname = drinks['strDrink']
drinkimage = drinks['strDrinkThumb']
instructions = drinks['strInstructions']
msg = f"**Drink**:\n {drinkname}\n\n **Ingredients**:\n {ingredients_list}\n **Instructions**: \n{instructions}"
print(msg)