Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
RPython将带引号的字符串从R复制到Python_Python_R_String_Rpython - Fatal编程技术网

RPython将带引号的字符串从R复制到Python

RPython将带引号的字符串从R复制到Python,python,r,string,rpython,Python,R,String,Rpython,我在使用RPython将R中的字符串复制到Python中的字符串时遇到问题。我知道如果字符串没有引号,但如果字符串有引号,则会出错,那么执行此操作的基本步骤是什么 例如: test <- "byte" python.assign("string",test) python.exec("print(string)") library('rPython') python.exec("myCommand=\"gdal_translate -of GTiff -ot Int16 -a_nodat

我在使用RPython将R中的字符串复制到Python中的字符串时遇到问题。我知道如果字符串没有引号,但如果字符串有引号,则会出错,那么执行此操作的基本步骤是什么

例如:

test <- "byte"
python.assign("string",test)
python.exec("print(string)")
library('rPython')
python.exec("myCommand=\"gdal_translate -of GTiff -ot Int16 -a_nodata \" + chr(39) +\"-32768\" + chr(39) + \" NETCDF:\" + chr(39) + \"inputFile\" + chr(39) + \":Soil_Moisture \" + chr(39) + \"outputFile\" + chr(39)")
python.get("myCommand")
[1] "gdal_translate -of GTiff -ot Int16 -a_nodata '-32768' NETCDF:'inputFile':Soil_Moisture 'outputFile'"

test我也在寻找这个问题的好答案。
我只能建议解决方法:

您的示例:

library('rPython')
python.exec("string=chr(39).join(['byte'])")
python.get("string")
[1] "byte"
# Alternatively: python.exec("string=chr(39) + \"byte\" + chr(39)")
# to get: [1] "'byte'"    
更复杂的事情:假设“我想要”这样的“东西”

library('rPython')
python.exec("string=\"Suppose \" + chr(39) + \"I want\" + chr(39) + \" \" + chr(34) + \"something\" + chr(34) + \" like \" + chr(39) + \"this\" + chr(39)")
python.get("string")
[1] "Suppose 'I want' \"something\" like 'this'"
另一个例子:

test <- "byte"
python.assign("string",test)
python.exec("print(string)")
library('rPython')
python.exec("myCommand=\"gdal_translate -of GTiff -ot Int16 -a_nodata \" + chr(39) +\"-32768\" + chr(39) + \" NETCDF:\" + chr(39) + \"inputFile\" + chr(39) + \":Soil_Moisture \" + chr(39) + \"outputFile\" + chr(39)")
python.get("myCommand")
[1] "gdal_translate -of GTiff -ot Int16 -a_nodata '-32768' NETCDF:'inputFile':Soil_Moisture 'outputFile'"

借用@matandked,您可以使用
gsub
将单引号替换为
chr(39)

library(rPython)

test <- "'byte'"

python.assign("string", gsub("\\'", "' + chr(39) + '", test))
python.get("string")

# [1] "'byte'"

我从未用过RPython,但你试过“\\'byte\\'”吗?(我想这会告诉python引用已经存在)谢谢你的建议。不幸的是,我仍然得到了上一个示例中的错误:SyntaxError:invalid syntaxI使用matiasg通知复制字符串{“byte”}(即带双引号的字符串)。所以如果我说:测试
library('rPython')
python.exec("string=\"Suppose \" + chr(39) + \"I want\" + chr(39) + \" \" + chr(34) + \"something\" + chr(34) + \" like \" + chr(39) + \"this\" + chr(39)")
python.get("string")
[1] "Suppose 'I want' \"something\" like 'this'"
library('rPython')
python.exec("myCommand=\"gdal_translate -of GTiff -ot Int16 -a_nodata \" + chr(39) +\"-32768\" + chr(39) + \" NETCDF:\" + chr(39) + \"inputFile\" + chr(39) + \":Soil_Moisture \" + chr(39) + \"outputFile\" + chr(39)")
python.get("myCommand")
[1] "gdal_translate -of GTiff -ot Int16 -a_nodata '-32768' NETCDF:'inputFile':Soil_Moisture 'outputFile'"
library(rPython)

test <- "'byte'"

python.assign("string", gsub("\\'", "' + chr(39) + '", test))
python.get("string")

# [1] "'byte'"
python.assign <- function (var.name, value, ...) 
{
  value <- gsub("\\'", "' + chr(39) + '", value) ## Added this line...
  value <- toJSON(value, collapse = "", ...)
  python.command <- c(paste(var.name, "='", value, "'", sep = " "), 
                      paste(var.name, "= json.loads(", var.name, ")", sep = ""), 
                      paste("if len(", var.name, ") == 1:", sep = ""), paste("    ", 
                                                                             var.name, "=", var.name, "[0]"))
  python.command <- paste(python.command, collapse = "\n")
  python.exec(python.command)
  invisible(NULL)
}


## Now there's no need to substitute single quotes:
python.assign("string", test)
python.get("string")

# [1] "'byte'"