将列表转换为可分割的浮点数
Turn A List to a Float That Can Be Divided
我一直在非常努力地为世界制作一个货币转换器。我一直面临的众多问题之一是我的货币转换器无法计算出汇率本身;你不得不。但当然,我想通了。
但我的问题是:我得到了欧元的汇率,但它是一个列表,我需要一个浮点数来进行计算。我该怎么做?
这是我试过的方法:
euro_exchange = float(str(tree.xpath('//div[@class="price"]/text()')))
######################################################################
euro_exchange = tree.xpath('//div[@class="price"]/text()')
float(str(euro_exchange)
################################################################
euro_exchange = float(tree.xpath('//div[@class="price"]/text()')
你明白了。当我尝试 euro_exchange = float(str(tree.xpath('//div[@class="price"]/text()')))
时,它说(我正在使用 TkInter,顺便说一句):
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.5/tkinter/__init__.py", line 1562, in __call__
return self.func(*args)
File "/home/jboyadvance/Documents/Code/Python/Currency Converter/Alpha2/main.py", line 21, in usd_callback
euro_exchange = float(str(tree.xpath('//div[@class="price"]/text()')))
ValueError: could not convert string to float: "['1.1394']"
当我尝试 euro_exchange = tree.xpath('//div[@class="price"]/text()')
float(str(euro_exchange)
时,我得到了相同的结果。
当我尝试时 euro_exchange = float(tree.xpath('//div[@class="price"]/text()')
:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.5/tkinter/__init__.py", line 1562, in __call__
return self.func(*args)
File "/home/jboyadvance/Documents/Code/Python/Currency Converter/Alpha2/main.py", line 21, in usd_callback
euro_exchange = float(tree.xpath('//div[@class="price"]/text()'))
TypeError: float() argument must be a string or a number, not 'list'
这是源代码:
import tkinter as tk
from lxml import html
import requests
window = tk.Tk()
window.title("Currency Converter")
window.geometry("500x500")
window.configure(bg="#900C3F")
# window.wm_iconbitmap("penny.ico")
page = requests.get('https://www.bloomberg.com/quote/EURUSD:CUR')
tree = html.fromstring(page.content)
def usd_callback():
usd_amount = float(ent_usd.get())
euro_exchange = float(str(tree.xpath('//div[@class="price"]/text()')))
euro_amount = usd_amount / euro_exchange
lbl_euros.config(text="Euro Amount: %.2f€" % euro_amount)
lbl_usd = tk.Label(window, text="Enter the USD ($) here:", bg="#900C3F", font="#FFFFFF")
ent_usd = tk.Entry(window)
btn_usd = tk.Button(window, text="Convert", command=usd_callback, bg="#FFFFFF", font="#FFFFFF")
lbl_euros = tk.Label(window)
lbl_usd.pack()
ent_usd.pack()
btn_usd.pack()
window.mainloop()
如有任何帮助,我们将不胜感激!谢谢!!
您需要将 return 值的第一个元素从 xpath
转换为:
euro_exchange = tree.xpath('//div[@class="price"]/text()')
euro_exchange = float(str(euro_exchange[0]))
我一直在非常努力地为世界制作一个货币转换器。我一直面临的众多问题之一是我的货币转换器无法计算出汇率本身;你不得不。但当然,我想通了。
但我的问题是:我得到了欧元的汇率,但它是一个列表,我需要一个浮点数来进行计算。我该怎么做?
这是我试过的方法:
euro_exchange = float(str(tree.xpath('//div[@class="price"]/text()')))
######################################################################
euro_exchange = tree.xpath('//div[@class="price"]/text()')
float(str(euro_exchange)
################################################################
euro_exchange = float(tree.xpath('//div[@class="price"]/text()')
你明白了。当我尝试 euro_exchange = float(str(tree.xpath('//div[@class="price"]/text()')))
时,它说(我正在使用 TkInter,顺便说一句):
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.5/tkinter/__init__.py", line 1562, in __call__
return self.func(*args)
File "/home/jboyadvance/Documents/Code/Python/Currency Converter/Alpha2/main.py", line 21, in usd_callback
euro_exchange = float(str(tree.xpath('//div[@class="price"]/text()')))
ValueError: could not convert string to float: "['1.1394']"
当我尝试 euro_exchange = tree.xpath('//div[@class="price"]/text()')
float(str(euro_exchange)
时,我得到了相同的结果。
当我尝试时 euro_exchange = float(tree.xpath('//div[@class="price"]/text()')
:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.5/tkinter/__init__.py", line 1562, in __call__
return self.func(*args)
File "/home/jboyadvance/Documents/Code/Python/Currency Converter/Alpha2/main.py", line 21, in usd_callback
euro_exchange = float(tree.xpath('//div[@class="price"]/text()'))
TypeError: float() argument must be a string or a number, not 'list'
这是源代码:
import tkinter as tk
from lxml import html
import requests
window = tk.Tk()
window.title("Currency Converter")
window.geometry("500x500")
window.configure(bg="#900C3F")
# window.wm_iconbitmap("penny.ico")
page = requests.get('https://www.bloomberg.com/quote/EURUSD:CUR')
tree = html.fromstring(page.content)
def usd_callback():
usd_amount = float(ent_usd.get())
euro_exchange = float(str(tree.xpath('//div[@class="price"]/text()')))
euro_amount = usd_amount / euro_exchange
lbl_euros.config(text="Euro Amount: %.2f€" % euro_amount)
lbl_usd = tk.Label(window, text="Enter the USD ($) here:", bg="#900C3F", font="#FFFFFF")
ent_usd = tk.Entry(window)
btn_usd = tk.Button(window, text="Convert", command=usd_callback, bg="#FFFFFF", font="#FFFFFF")
lbl_euros = tk.Label(window)
lbl_usd.pack()
ent_usd.pack()
btn_usd.pack()
window.mainloop()
如有任何帮助,我们将不胜感激!谢谢!!
您需要将 return 值的第一个元素从 xpath
转换为:
euro_exchange = tree.xpath('//div[@class="price"]/text()')
euro_exchange = float(str(euro_exchange[0]))