rPython method python.get returns weird encoding

96 Views Asked by At

I m trying to use rPython package to pass some arguments into python code and get results back. But for some reason I m getting weird encoding from my python code. Maybe someone has some hints to point me out.

Here is my simple code to test:

require(rPython)

#pass the test word 'audiention' (in ukrainian)
word<-"аудієнція"
python.assign("input", word)
python.exec("input = input.encode('utf-8')")   
python.exec("print input") #the output in console is correct at this step: аудієнція
x<-python.get("input")
cat(x) # the output is: 0C4VT=FVO

Does anybody have some suggestions why the output of python.get is encoded weird?

My Sys.getlocale() output is:

Sys.getlocale()
[1] "LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=uk_UA.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=uk_UA.UTF-8;LC_MESSAGES=en_US.UTF-8;LC_PAPER=uk_UA.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=uk_UA.UTF-8;LC_IDENTIFICATION=C"

Thank you in advance for any hints!

1

There are 1 best solutions below

0
On

I have recently built a new package based on the original rPython code called SnakeCharmR that addresses this and other problems rPython had.

A quick comparison:

> library(SnakeCharmR)
> py.assign("a", "'")
> py.get("a")
[1] "'"
> py.assign("a", "áéíóú")
> py.get("a")
[1] "áéíóú"

> library(rPython)
> python.assign("a", "'")
  File "<string>", line 2
    a =' [ "'" ] '
                 ^
SyntaxError: EOL while scanning string literal
> python.assign("a", "áéíóú")
> python.get("a")
[1] "\xe1\xe9\xed\xf3\xfa"

You can install SnakeCharmR like this:

> library(devtools)
> install_github("asieira/SnakeCharmR")

Hope this helps.