如何自动将NoneType转换为字符串?

时间:2017-06-16 18:01:57

标签: 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 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

然而,这真的很奇怪,因为它的工作时间是一半,而另一半却没有。为什么其余的工作,但这个特定的不工作?

1 个答案:

答案 0 :(得分:1)

我认为这会有效..

只需使用此功能Share代替from yahoo_finance import Share def Share_nonesafe(x): if Share(x) == None: return (' price not available.') else: return (Share(x))

firstViewController