Reputation: 31
have a file tmodule.py
contains a dictionary of URL's.when try to call it with specific URL id, get an error.
code looks like below:
import pandas as pd
import csv
import requests
def stock(self, stockname="", value=100):
stocknames = {
"ABAD1": "59612098290740355",
"ABDI1": "49054891736433700",
}
urlid = stocknames[stockname]
url = 'http://.............................&i=' + urlid
content = requests.get(url)
with open('out.csv', 'w') as f:
writer = csv.writer(f)
for line in content.iter_lines():
writer.writerow(line.decode('utf-8').split(','))
df = pd.read_csv('out.csv')
return (....)
above code has no error but when i try to pass a key like:
import tmodule as tm
st=tm.stock("ABAD1")
got this error :
File "D:\anaconda\envs\geo_env\Lib\site-packages\..........", line 323, in stock
urlid = stocknames[stockname]
KeyError: ''
it's a long dictionary and i just copy two key and value here
Upvotes: 0
Views: 669
Reputation: 14253
It looks this is regular function, not a method in a class (as implied by first parameter self
). When you pass "ABAD1"
as positional argument when call st=tm.stock("ABAD1")
it is bind to self
and stockname
remains with default value of ""
.
Unless you provide a reason for self
being first parameter, just remove it.
def stock(stockname="", value=100):
Upvotes: 1