Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何自动将非类型转换为字符串?_Python_Yahoo Finance - Fatal编程技术网

Python 如何自动将非类型转换为字符串?

Python 如何自动将非类型转换为字符串?,python,yahoo-finance,Python,Yahoo Finance,这是我的代码: from yahoo_finance import Share from pprint import pprint laz = Share('LAZ') #Lazard # ABERDEEN amg = Share('AMG') #Affiliated Managers Group ben = Share('BEN') #Franklin Resources lm = Share('LM') #Legg Mason evr = Share('EVR') #Evercore Pa

这是我的代码:

from yahoo_finance import Share
from pprint import pprint

laz = Share('LAZ') #Lazard
# ABERDEEN
amg = Share('AMG') #Affiliated Managers Group
ben = Share('BEN') #Franklin Resources
lm = Share('LM') #Legg Mason
evr = Share('EVR') #Evercore Partners
ghl = Share('GHL') #Greenhill
hli = Share('HLI') #Houlihan Lokey
mc = Share('MC') #Moelis
pjt = Share('PJT') #PJT Partners
ms = Share('MS') #Morgan Stanley
gs = Share('GS') #Goldman Sachs
jpm = Share('JPM') #JP Morgan
ab = Share('AB') #Alliance Bernstein

print ("Lazard: $" + laz.get_open())
# ABERDEEN
print ("AMG: $" + amg.get_open())
print ("Franklin: $" + ben.get_open())
print ("LeggMason: $" + lm.get_open())
print ("Evercore: $" + evr.get_open())
print ("Greenhill: $" + ghl.get_open())
print ("Houlihan: $" + hli.get_open())
print ("Moelis: $" + mc.get_open())
print ("PJT: $" + pjt.get_open())
print ("MorganStanley: $" + ms.get_open())
print ("Goldman: $" + gs.get_open())
print ("JPMorgan: $" + jpm.get_open())
print ("AllianceBernstein: $" + ab.get_open())
这就是我得到的错误:

Traceback (most recent call last):
  File "C:/Users/ballz/Documents/Python/PDF to Excel/StockPerformance/stockcompetitoranalyis.py", line 26, in <module>
    print ("Houlihan: $" + hli.get_open())
TypeError: must be str, not NoneType
回溯(最近一次呼叫最后一次):
文件“C:/Users/ballz/Documents/Python/PDF to Excel/StockPerformance/stockcompetitoranalyis.py”,第26行,在
打印(“Houlihan:$”+hli.get\u open())
TypeError:必须是str,而不是NoneType

然而,这真的很奇怪,因为它有一半时间工作,另一半时间不工作。为什么剩下的部分可以工作,但这个特定的部分不行呢?

我认为这会起作用

只需使用此功能
Share\u nonesafe
而不是
Share

from yahoo_finance import Share

def Share_nonesafe(x):
    if Share(x) == None:
        return (' price not available.')        
    else:
        return (Share(x))

尝试强制转换:
str(hli.get\u open())
您希望在该实例中发生什么?它应该打印
'None'
、空字符串(
'
)或其他内容吗?非类型对象(无,因为它是唯一的非类型对象)与所需字符串无关,并且不能用于检索所需字符串。对其调用
str
只会产生字符串
“None”
。可能数据不可用。也许你达到了利率限制或者其他什么。不管是什么情况,您都没有数据。使用
str().format()
方法不会抛出错误,但字符串只会读取“None”,这是因为在这种情况下没有数据,
get\u open()
方法返回None。