Hello, I need to convert an R vector to a python array. Essentially, I got a vector of strings where each element must be enclosed in single quotes. The problem is that each closing single quote should contain a comma. What is the regex trick to do that? I tried with: ```> (raws = c("field_1", "field_2", "field_3"))[1] "field_1" "field_2" "field_3"> (items = sQuote(raws))[1] "'field_1'" "'field_2'" "'field_3'"> gsub("\'[:blank:]", "\',[:blank:]", items, ignore.case = FALSE, perl = FALSE)[1] "'field_1'" "'field_2'" "'field_3'"> cat("python_Array = [", domain_list, "]\n")python_Array = [ 'Index' 'Endpoints' 'Confounders' 'Arboexposure' ] ``` To note that `python_Array` does not have commas between elements and `gsub` did not do anything... Thank you
Hello ... raws <- c("field_1", "field_2", "field_3") paste0("['", paste0(raws, collapse="', '"), "']") rasmus at eightforty ~ % python Python 3.10.5 (main, Jun 6 2022, 18:49:26) [GCC 12.1.0] on linux Type "help", "copyright", "credits" or "license" for more information.>>> ['field_1', 'field_2', 'field_3']['field_1', 'field_2', 'field_3'] ? R
On Fri, 10 Jun 2022 12:19:57 +0200 Luigi Marongiu <marongiu.luigi at gmail.com> wrote:> I need to convert an R vector to a python array.Have you considered using the reticulate package on the R side or the rpy2 package on the Python side? It's hard to cover all edge cases when producing source code to be evaluated by another language. You may encounter a corner case where you output a character special to Python without properly escaping it and end up with an injection error (see also: SQL injection vulnerability, the most widely known type of this mistake). For producing R code from R values, there's deparse(), and even that's not perfect: https://bugs.r-project.org/show_bug.cgi?id=18232 Having said that, paste(sQuote(values, FALSE), collapse = ',') will do the trick, but only if values are guaranteed not to contain single quotes or other characters that have special meaning in Python. Note the FALSE argument to sQuote: otherwise it could return ?Unicode quotes?, `TeX quotes', or even ?guillemets?, depending on the options set by the user. -- Best regards, Ivan
Hello, cat has a sep argument: capture.output(cat(raws, sep = ",")) #[1] "field_1,field_2,field_3" Instead of capture.output there is also cat argument file. Hope this helps, Rui Barradas ?s 11:19 de 10/06/2022, Luigi Marongiu escreveu:> Hello, > I need to convert an R vector to a python array. Essentially, I got a > vector of strings where each element must be enclosed in single > quotes. The problem is that each closing single quote should contain a > comma. What is the regex trick to do that? > I tried with: > ``` >> (raws = c("field_1", "field_2", "field_3")) > [1] "field_1" "field_2" "field_3" >> (items = sQuote(raws)) > [1] "'field_1'" "'field_2'" "'field_3'" >> gsub("\'[:blank:]", "\',[:blank:]", items, ignore.case = FALSE, perl = FALSE) > [1] "'field_1'" "'field_2'" "'field_3'" >> cat("python_Array = [", domain_list, "]\n") > python_Array = [ 'Index' 'Endpoints' 'Confounders' 'Arboexposure' ] > ``` > To note that `python_Array` does not have commas between elements and > `gsub` did not do anything... > > Thank you > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.