在R中用reticulate调用comtradeapicall API:None/True定义问题
在R中用reticulate调用Python的comtradeapicall API时,正确传递None/True等Python对象的方法
问题背景
使用reticulate调用Python的comtradeapicall API时,直接在R代码中写Python的None会触发Error: object 'None' not found错误;若将None设为字符串"None",则会导致API调用失败。需要找到正确传递Python原生的None、True、False对象的方式。
解决方案
reticulate内置了专门函数生成对应的Python原生特殊对象:
- 用
py_none()替代Python中的None - 用
py_true()替代Python中的True - 用
py_false()替代Python中的False
修改后的R代码示例
subscription_key = "xxxxx1234..." # 已确认可用 ctdr = import("comtradeapicall") # 加载Python包comtradeapicall # 使用py_none()传递Python的None对象 mydf = ctdr$getFinalDataAvailability(subscription_key, typeCode='C', freqCode='A', clCode='HS', period='2021', reporterCode=py_none())
说明
这些函数生成的是Python原生特殊对象,传递给API时能被正确识别,不会出现类型不匹配问题,适合批量API调用场景统一使用。
内容的提问来源于stack exchange,提问作者econ0101
相关产品推荐
相关产品推荐

