[英]I am confused why I am getting so many error messages after trying to use split and adding a string to json output python
我在處理這段代碼時遇到了問題。 我正在使用 OpenWeather API 來獲取天氣數據,這一切都很好。 但是當這個塊執行時, current_temperature 代碼塊的前兩行工作正常,但是當我嘗試拆分它時,它返回一個錯誤。 我還嘗試將度數符號添加到末尾,這會返回錯誤。
使用 current_pressure,我嘗試添加 hPa 后綴並返回錯誤。 有任何想法嗎?? 我真的需要這個工作。
import json, requests
current_temperature = y['temp']
current_temperature = (current_temperature - 273.15) * 9/5 + 32
current_temperature = current_temperature.split(5,-1)
current_temperature = current_temperature + '°'
current_pressure = y['pressure']
current_pressure = current_pressure + ' hPa'
謝謝!
您正在尋找的是首先舍入(或截斷,但在下面的示例中,我舍入)數字,然后將其轉換為字符串,以便您可以格式化 output。 你可以這樣做:
current_temperature = y['temp']
current_temperature = (current_temperature - 273.15) * 9/5 + 32
current_temperature = round(current_temperature, 2)
current_temperature = f'{current_temperature}°'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.