How to import a list of RICs from csv file, then use it in ek.get_data

I have been trying to import a list of Cusips from csv file, but then I want to use it in ek.get_data funtion insted of using a list of cusips. This is the code I have so far :

I want to do this :

data_grid, err = ek.get_data([CUSIPS], ['DSPLY_NAME','OFFCL_CODE',.....]

Where CUSIPS is a csv file

insted of this:

data_grid, err = ek.get_data(['USFN3025=RR', 'USFN3030=RR'],['OFFCL_CODE','DSPLY_NAME'])

Thanks,


Best Answer

  • Hi @GABRIEL.ESQUIVEL,

    There are a couple of things within your code segment above:

    1. You are not creating an array of items, but an array of rows of data. You will have to loop through each row to pull out the item.

    2. When you call get_data(), instead of this:

    ek.get_data(["instrument"], ...)

    you need to do this:

    eg.get_data(instrument, ...)

    All you are doing above is passing in an array containing the value "instrument". The variable you created above called instrument is an array and you simply need to pass that in.

    Here is a code sample that works for me. Note: you can see I'm looping through each row and pulling out item:

    image

Answers

  • Hi @GABRIEL.ESQUIVEL,

    The get_data() function does not accept a file as input. You will have to import your file into an array and provide that as input. Because CSV files can contain whatever you want, comments, blank lines, etc, it is more conducive for the developer to parse the file themselves. You can likely find numerous examples on the internet such as this one. If you have more complex layouts within your CSV file, the pandas read_csv may be useful.

    In either case, it is a few extra lines of code.

  • Yes, you're right but how I introduce this array into the funtion?

    I doing the following:

    instrument = []

    with open('CUSIPS - NONEST.csv') as csvDataFile:
    csvReader = csv.reader(csvDataFile)
    for row in csvReader:
    instrument.append(row[0])

    print(instrument)

    then,

    Prices_NonEstrutured, err = ek.get_data(["instrument"],
    ['OFFCL_CODE','DSPLY_NAME'])
    Prices_NonEstrutured.head()

    But of course this wont work

    Thank you @nick.zincone.1


  • Hi @nick. These was very helpful, thank you so much for you're time :)