Using Python dict values from external file in WRDS CRSP query

喜夏-厌秋 提交于 2021-01-28 11:14:55

问题


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:


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()


来源:https://stackoverflow.com/questions/60100474/using-python-dict-values-from-external-file-in-wrds-crsp-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!