1

I would like to read dictionary values (CRSP PERMNOS) from an external file (a Python dict) and use them in a function that will return rows from a particular table. The code below, adapted from the WRDS Python Support site, works well with the PERMNO values written inline.

def main():
    parm = {'permnos': ('22074', '20482', '10049', '19641', '18980')}
    data = db.raw_sql('SELECT date,permno,comnam,cusip FROM crsp.dseall WHERE permno in %(permnos)s', params=parm)
    data.to_csv(r'cusip-result-2.csv')
    print(data)
if __name__=="__main__":
    main()

However, I have 541 PERMNOS and would prefer to avoid entering them manually inline!

Thanks for any tips or pointers!

1 Answers1

0

First, write each of your permnos to a file called permnos.txt in the following format:

22074
20482
...
18980

Then you can do the following:

def main():
    parm = {'permnos': tuple([line for line in open('permnos.txt', 'r')])}
    data = db.raw_sql('SELECT date,permno,comnam,cusip FROM crsp.dseall WHERE permno in %(permnos)s', params=parm)
    data.to_csv(r'cusip-result-2.csv')
    print(data)
if __name__=="__main__":
    main()
Greg
  • 1,845
  • 2
  • 16
  • 26