我有一本字典,我想使用此代碼將數據寫入文件currencies.csv: 爲字典的代碼是節省字典到CSV文件
def _fetch_currencies():
response =urllib.request.urlopen('http://www.maths.manchester.ac.uk/~vsego/python/currencies.json')
html = response.read().decode("utf-8")
currencies = json.loads(html)
pprint(currencies)
def _save_currencies(currencies):
with open(('currencies.csv'), 'wt') as my_csv:
csv_writer = csv.writer(my_csv, delimiter=',')
csv_writer.writerow(["code","name"])
for key,value in currencies.items():
csv_writer.writerow([key,value])
_save_currencies(_fetch_currencies())
當我回到我的代碼得到錯誤信息
'AttributeError: 'NoneType' object has no attribute 'items''
我該如何解決這個錯誤?
什麼是貨幣? –
我的Python有點生疏,但'_fetch_currencies()'不返回任何東西。因此,它的調用應該分別評估爲'null'或'NoneType',這就是交給'_save_currencies()'的東西。由於'NoneType'沒有'items()'方法,拋出異常。 –