从R调用Python - 使用双引号

时间:2015-08-26 06:30:27

标签: python r escaping

我写了一个Python函数,它允许我对任何文件夹中的所有文件重复我的命令:

import os

def removeChar(stringVar,removeChars):
    for znaki in removeChars:
        stringVar=stringVar.replace(znaki,'')
    return stringVar

def ForAllFiles(path,command='gdal2xyz.py "inputFile" "outputFile"',removeChars=[],extension='.tif',newExtension='.txt'):
    for root, dirs, files in os.walk(path):
        dirs.sort()
        for file in files:
            if file.endswith(extension):
                newFile=removeChar(file,removeChars)
                newFile=newFile.replace(extension,newExtension)
                cmd=command.replace('inputFile',os.path.join(root,file))
                cmd=cmd.replace('outputFile',os.path.join(root,newFile))
                os.system(cmd)

我希望在R中分配一些Python变量(路径,命令)!然后从R执行此功能! 其中一个提到的变量包含带文件名的命令(' inputFile'),所以我希望用双引号括起文件名。

我打电话的时候:

library(rPython)
python.assign("variable",'my "variable"')

我收到了一个错误:

wpython.exec(python.command):   期待,分隔符:第1行第9列(字符8)

所以我认为逃避双重报价就足够了:

python.assign("variable",'my \"variable\"')
python.assign("variable","my \"variable\"")

但它也不起作用并产生错误信息

所以我的最后一个想法是:

python.assign("variable","my " + python.exec("chr(34)") + "variable" + python.exec("chr(34)"))

但也失败了

0 个答案:

没有答案
相关问题