我收到错误:json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
,在我的小型Python程序中(在Jupyter Notebook上),阻止我实际计算货币。
它应该是一个简单的货币转换器应用程序;但是,当我在“转换”中使用计算代码时,函数然后我得到上面的错误,否则在函数之外,代码很好地使用API密钥检索数据并转换货币。
提前感谢大家提供任何帮助,因为我真的很新,我的代码上的任何其他建议(也许不是以 pythonic方式完成)将不胜感激
> Exception in Tkinter callback
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
File "<ipython-input-41-161d07d3ddae>", line 29, in convert
currency_Collection = json.loads(str(r.text))
File "C:\ProgramData\Anaconda3\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\ProgramData\Anaconda3\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\ProgramData\Anaconda3\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
#-------------Imports
import requests
import json
from tkinter import *
from tkinter import ttk
#-------------Variables
value_ToConvert = float()
value_Converted = float()
currencyBase_ToConvert = ''
currencyBase_Converted = ''
countries = ['ALL: Albania [Lek]','AMD: Armenia [Armenian Dram]','ANG: Curaço [Netherlands Antillean Guilder]','AOA: Angola [Kwanza]','ARS: Argentina [Argentine Peso]','AUD: Australia [Australian Dollar]','AZN: Azerbaijan [Azerbaijanian Manat]','BBD: Barbados [Barbados Dollar]','BDT: Bangladesh [Taka]','BGN: Bulgaria [Bulgarian Lev]','BHD: Bahrain [Bahraini Dinar]','BRL: Brazil [Brazilian Real]','BSD: Bahamas [Bahamian Dollar]','BWP: Botswana [Pula]','BYN: Belarus [Belarusian Ruble]','CAD: Canada [Canadian Dollar]','CDF: Congo [Franc Congolais]','CLP: Chile [Chilean Peso]','CNY: China [Yuan Renminbi]','COP: Colombia [Colombian Peso]','CZK: Czech Republic [Czech Koruna]','DKK: Denmark [Danish Krone]','DOP: Dominican Republic [Dominican Peso]','EGP: Egypt [Egyptian Pound]','ETB: Ethiopia [Ethiopian Birr]','EUR: Åland Islands [Euro]','FJD: Fiji [Fiji Dollar]','GBP: Guernsey [Pound Sterling]','GEL: Georgia [Lari]','GHS: Ghana [Ghana Cedi]','GTQ: Guatemala [Quetzal]','HKD: Hong Kong [Hong Kong Dollar]','HNL: Honduras [Lempira]','HRK: Croatia [Croatian Kuna]','HUF: Hungary [Forint]','IDR: Indonesia [Rupiah]','ILS: Israel [New Israeli Sheqel]','INR: India [Indian Rupee]','IQD: Iraq [Iraqi Dinar]','IRR: Iran, Islamic Republic Of [Iranian Rial]','ISK: Iceland [Iceland Krona]','JMD: Jamaica [Jamaican Dollar]','JOD: Jordan [Jordanian Dinar]','JPY: Japan [Yen]','KES: Kenya [Kenyan Shilling]','KHR: Cambodia [Riel]','KRW: Korea, Republic of [Won]','KWD: Kuwait [Kuwaiti Dinar]','KZT: Kazakhstan [Tenge]','LAK: Lao Peoples Democratic Republic [Kip]','LBP: Lebanon [Lebanese Pound]','LKR: Sri Lanka [Sri Lanka Rupee]','MAD: Morocco [Moroccan Dirham]','MDL: Moldova, Republic of [Moldovan Leu]','MKD: Macedonia [Denar]','MMK: Myanmar [Kyat]','MUR: Mauritius [Mauritius Rupee]','MXN: Mexico [Mexican Peso]','MYR: Malaysia [Malaysian Ringgit]','NAD: Namibia [Namibia Dollar]','NGN: Nigeria [Naira]','NOK: Norway [Norwegian Krone]','NZD: New Zealand [New Zealand Dollar]','OMR: Oman [Rial Omani]','PAB: Panama [Balboa]','PEN: Peru [Nuevo Sol]','PGK: Papua New Guinea [Kina]','PHP: Philippines [Philippine Peso]','PKR: Pakistan [Pakistan Rupee]','PLN: Poland [Zloty]','PYG: Paraguay [Guarani]','QAR: Qatar [Qatari Rial]','RON: Romania [New Romanian Leu]','RSD: Serbia [Serbian Dinar]','RUB: Russian Federation [Russian Ruble]','SAR: Saudi Arabia [Saudi Riyal]','SCR: Seychelles [Seychelles Rupee]','SEK: Sweden [Swedish Krona]','SGD: Singapore [Singapore Dollar]','THB: Thailand [Baht]','TJS: Tajikistan [Somoni]','TND: Tunisia [Tunisian Dinar]','TRY: Turkey [New Turkish Lira]','TTD: Trinidad and Tobago [Trinidad and Tobago Dollar]','TWD: Taiwan, Province of China [New Taiwan Dollar]','TZS: Tanzania [Tanzanian Shilling]','UAH: Ukraine [Hryvnia]','USD: United States [US Dollar]','UYU: Uruguay [Peso Uruguayo]','UZS: Uzbekistan [Uzbekistan Sum]','VEF: Venezuela [Bolivar]','VND: Viet Nam [Dong]','XAF: Cameroon [CFA Franc BEAC ‡]','XCD: Anguilla [East Caribbean Dollar]','XOF: Benin [CFA Franc BCEAO †]','XPF: French Polynesia [CFP Franc]','ZAR: South Africa [Rand]','ZMW: Zambia [Kwacha]']
#-------------Definitions
try:
def convert():
value_ToConvert = value_entry.get()
currencyBase_ToConvert = currencyBase_ToConvert_Box.get()
currencyBase_Converted = currencyBase_Converted_Box.get()
currencyBase_Parameter = '/' + currencyBase_ToConvert
r = requests.get('https://v3.exchangerate-api.com/bulk/b76715d91ecbbc9c14a229e8' + currencyBase_Parameter)
currency_Collection = dict()
currency_Collection = json.loads(r.text)
currency_Rates = dict(currency_Collection['rates'])
value_Converted = float(value_ToConvert) * float(currency_Rates[currencyBase_Converted])
print (value_Converted)
currencyBase_Converted_Label = ttk.Label(mainframe, text = value_Converted).grid(column = 3, row = 2, stick = W, padx = 10, pady = 20)
except Exception:
print(Exception)
def reset():
value_ToConvert = 0.0
value_Converted = 0.0
currencyBase_ToConvert = ''
currencyBase_Converted = ''
currencyBase_ToConvert_Box.set("")
currencyBase_Converted_Box.set("")
return
def exit():
root.destroy()
return
#-------------Creating Frame
root = Tk()
root.title("Currency Converter")
mainframe = ttk.Frame(root, padding = "3 3 12 12")
root.geometry ('350x200+0+0')
mainframe.grid(column = 0, row = 0, sticky = (N, W, E, S))
mainframe.columnconfigure(0, weight = 1)
mainframe.rowconfigure(0, weight = 1)
#Row: 1
ttk.Label(mainframe, text = "HAVE").grid(column = 1, row = 1, sticky = (N,W), padx = 10, pady = 20)
currencyBase_ToConvert_Box = ttk.Combobox(mainframe, textvariable = currencyBase_ToConvert, state = 'readonly')
currencyBase_ToConvert_Box['values'] = countries
currencyBase_ToConvert_Box.current(0)
currencyBase_ToConvert_Box.grid(column = 2, row = 1, padx = 0, pady = 20)
currencyBase_ToConvert_Box.set("")
value_entry = ttk.Entry(mainframe, width = 10, textvariable = value_ToConvert)
value_entry.grid(column = 3, row = 1, sticky = (N,E), padx = 10, pady = 20)
#Row: 2
ttk.Label(mainframe, text = "WANT").grid(column = 1, row = 2, stick = W, padx = 10, pady = 20)
currencyBase_Converted_Box = ttk.Combobox(mainframe, textvariable = currencyBase_Converted, state = 'readonly')
currencyBase_Converted_Box['values'] = countries
currencyBase_Converted_Box.current(0)
currencyBase_Converted_Box.grid(column = 2, row = 2, padx = 0, pady = 20)
currencyBase_Converted_Box.set("")
#Row: 3
btnConvert = Button(mainframe, text = "Convert", padx = 40, pady = 20, width = 1, height = 1, command = convert).grid(column = 1, row = 3)
btnReset = Button(mainframe, text = "Reset", padx = 40, pady = 20, width = 1, height = 1, command = reset).grid(column = 2, row = 3)
btnExit = Button(mainframe, text = "Exit", padx = 40, pady = 20, width = 1, height = 1, command = exit).grid(column = 3, row = 3)
root.mainloop()
我添加了一张图片,以帮助说明我尝试帮助的内容,输出(在激活“转换”按钮后创建)应位于右上角的标签下方,位于:第3栏;第2行。 enter image description here