win10toast无法转换为Unicode错误

win10toast can not be converted to Unicode error

所以我在 python 中制作了一个程序,基本上可以获取特定的股票价格,并且能够在程序 运行 显示该特定价格的计算机上弹出通知.但是当我这样做时出现错误。这是我的代码和错误。

from win10toast import ToastNotifier
from yahoo_fin import stock_info as si

from yahoo_fin.stock_info import get_analysts_info, get_company_info, get_company_officers, get_currencies, get_top_crypto
user_input = input("Company(refer as stock name): ")
stock_price = si.get_live_price(user_input)

toast = ToastNotifier()
toast.show_toast("test",stock_price, duration=20)

这是我的错误

  File "c:\Users\dilly\Downloads\notifier\main.py", line 9, in <module>
    toast.show_toast("test",stock_price, duration=20)
  File "C:\Users\dilly\AppData\Local\Programs\Python\Python310\lib\site-packages\win10toast\__init__.py", line 127, in show_toast
    self._show_toast(title, msg, icon_path, duration)
  File "C:\Users\dilly\AppData\Local\Programs\Python\Python310\lib\site-packages\win10toast\__init__.py", line 107, in _show_toast
    Shell_NotifyIcon(NIM_MODIFY, (self.hwnd, 0, NIF_INFO,
TypeError: Objects of type 'numpy.float64' can not be converted to Unicode.

can not be converted to Unicode 错误意味着代码需要一个字符串但得到了其他东西。

最简单的修复方法是直接将对象转换为字符串。

toast.show_toast("test", str(stock_price), duration=20)

更现代的方法是 f-strings which allow you to place the value inside another string if you want and apply optional formatting。例如:

toast.show_toast("test", f"Price: {stock_price:0.2f}", duration=20)