我有這樣的代碼塊,它應該給出來的時候,股票代碼提供了CIK號碼:輸入字符串參數作爲功能可變給人一種NameError
def lookup_cik(ticker, name=None):
good_read = False
ticker = ticker.strip().upper()
url = 'http://www.sec.gov/cgi-bin/browse-edgar?action+getcompany&CIK=(cik)&count=10&output=xml'.format(cik=ticker)
try:
xmlFile = urlopen (url)
try:
xmlData = xmlFile.read()
good_read = True
finally:
xmlFile.close()
except HTTPError as e:
print("HTTP Error:", e.code)
except URLError as e:
print("URL Error:", e.reason)
except TimeoutError as e:
print("Timeout Error:", e.reason)
except socket.timeout:
print("Socket Timeout Error")
if not good_read:
print("Unable to lookup CIK for ticker:", ticker)
return
try:
root = ET.fromstring(xmlData)
except ET.ParseError as perr:
print("XML Parser Error:", perr)
try:
cikElement = list(root.iter("CIK"))[0]
return int(cikElement.text)
except StopIteration:
pass
然而,當它試圖輸入一個股票股票我得到
>>> lookup_cik(BDX)
Traceback (most recent call last):
File "<pyshell#34>", line 1, in <module>
lookup_cik(BDX)
NameError: name 'BDX' is not defined
我知道這是一個NameError
,但我從來沒有見過其中函數不承認所謂輸入的參數數據的股票代碼在我們的例子是BDX一個問題。
如何定義BDX?你沒有引用它。 – octopusgrabbus