因此,我試圖使用美國國家航空航天局的API列印一些格式化的文本,列印空間中接近地球的物體的名稱和距離。
我的問題是,我無法在距離的前面獲得空位,(以便它們在名稱的旁邊獲得右對齊)我試過{:25d},但這不起作用,并給我帶來了
錯誤: print the name and distance in space.
錯誤: print(f "Name: {name:25} Entfernung km: {distance:25d}" ) ValueError: 對于'str'型別的物件,未知的格式代碼'd'
。python檔案的例子(https://docs.python.org/3/tutorial/inputoutput.html)作業起來沒有問題。
span class="hljs-keyword">import requests
key = "DEMO_KEY"/span>
start_date = "2021-03-20"/span>
end_date = "2021-03-27"/span>
r = requests.get(f "https://api.nasa.gov/neo/rest/v1/feed?start_date={start_date}& end_date={end_date}& api_key={key}")
json = r.json()
def search_name_distance(date)。
print(date, " 。
")
for x in range(len(json['near_earth_objects'][date]) 。)
name = json['near_earth_objects'][date] [x]['name']
距離 = json['near_earth_objects'][date][x]['close_approach_data'] [0]['miss_distance'][' kilometers']
print(f "名稱。{name:25} Entfernung km: {distance:25d}")
print("
")
search_name_distance(start_date)
''
作業。
table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
for name, phone in table.items():
print(f'{name:10} ==> {phone:10d}' )
''
uj5u.com熱心網友回復:
d是代表一個整數,但在你的例子中distance是一個str。所以你可以轉換
distance = int(json['near_earth_objects'][date][x]['close_approach_data'][0]['miss_distance'][' kilometers']
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/307853.html
標籤:
上一篇:如何根據子串對字串進行排序JS
