0

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

1

1 Answer 1

1

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):
Sign up to request clarification or add additional context in comments.

2 Comments

it's a python module and i installed it on my system and when delete self get another error : Traceback (most recent call last): File "D:/site-packages/....py", line 2, in <module> foolad=tm.stock("FOLD1") File "D:\asite-packages\\.....py", line 329, in stock writer.writerow(line.decode('utf-8').split(',')) File "C:\Users\.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError: 'charmap' codec can't encode character '\u06cc' in position 48: character maps to <undefined>
This is completely different error, not caused by removing self from your function, but how you process the response when write to file. Ask a separate question, providing minimal reproducible example, sample of the response you get, the full traceback, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.