python - 将 R 代码转换为 Python 的问题

标签 python r pip code-conversion

from pyensae.languages import r2python

print(r2python(rscript, pep8=True))

我在将 filename.R 转换为 pythonfilename.py 时遇到问题,因为这些库对转换没有用,因为即使我安装了它也会生成错误“找不到模块”该模块使用:

pip install pyensae

最佳答案

以下步骤适用于 Python 版本 Python 3.7.6

  1. 升级你的pip
python -m pip install --upgrade pip
  1. 安装pyensae模块
pip install pyensae
  1. 通过执行下面的 import 检查你的 python 控制台
from pyensae.languages import r2python

如果你面对

ModuleNotFoundError: No module named 'antlr4'

或面向

ModuleNotFoundError: No module named 'builtin'

然后执行下面的命令

pip install antlr4-python3-runtime

经过上述步骤,我可以将R脚本转换成python语言

rscript = """
nb=function(y=1930){
debut=1816
MatDFemale=matrix(D$Female,nrow=111)
colnames(MatDFemale)=(debut+0):198
cly=(y-debut+1):111
deces=diag(MatDFemale[:,cly[cly%in%1:199]])
return(c(B$Female[B$Year==y],deces))}
"""
from pyensae.languages import r2python
print(r2python(rscript, pep8=True))

控制台输出

ANTLR runtime and generated code versions disagree: 4.9.1!=4.8 ANTLR runtime and generated code versions disagree: 4.9.1!=4.8 from python2r_helper import make_tuple

def nb(y=1930):
    debut = 1816
    MatDFemale = matrix(D . Female, nrow=111)
    colnames(MatDFemale) .set(range((debut + 0), 198))
    cly = range((y - debut + 1), 111)
    deces = diag(MatDFemale[:, cly[set(cly) & set(range(1, 199))]])
    return make_tuple(B . Female[B . Year == y], deces)

关于python - 将 R 代码转换为 Python 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61039549/

相关文章:

python - 将包含字符串和字典的列表写入 csv 文件

python - 当行元素满足条件 Pandas 时查找列名

python - 如何在 twisted.web(或 twisted.web2)中添加钩子(Hook)?

R对象的名称承载多种功能

python - 在 Web2py 中访问安装在 Pythonanywhere 上的模块

python - 非阻塞套接字,错误总是

r - Zoo merge() 和合并的列名

r - 在R中,在数字字符串的正确位置添加逗号

python - 将输出文件添加到 Python 扩展

python - 使用 'pip' 使用插件安装 uwsgi