Read the encoding of an existing connection object

Is there a way to get (and set) encoding existing connection? For instance:

 con <- file(tempfile(), encoding = "UTF-8") summary(con) 

The summary lists the mode, and if it was open, but not the encoding used by the connection.

+6
source share
1 answer

I'm really not sure that I understand well what you need to do. But considering that

  • the connection is associated with an existing file on disk
  • you definitely need to read from the file
  • you might want to write to a file

Then you could do something like this if you forced UTF-8 encoding:

 # Hypothetical connection used by the user (file must exist on dist, hence # the "w" here con <- file(tempfile(), open = "w", encoding = "UTF-8") # recup the attributes of the existing connection con.attr <- summary(con) # build a list of parameters for a new connection that would replace # the original one newcon.attr <- list() newcon.attr["description"] <- con.attr$description newcon.attr["open"] <- paste0("r", ifelse(con.attr$'can write'=='yes', "+", "")) newcon.attr["encoding"] <- "UTF-8" # close the original connection, and create the new one close(con) newcon <- do.call(what = file, args = newcon.attr) # Check its attributes summary(newcon) # $description # [1] "C:\\Users\\...\\Temp\\Rtmpo9ykjo\\file54744993321b" # # $class # [1] "file" # # $mode # [1] "r+" # # $text # [1] "text" # # $opened # [1] "opened" # # $`can read` # [1] "yes" # # $`can write` # [1] "yes" 

To check whether the previous content was encoded using UTF-8 or not, this is a completely different story, so this may or may not be useful in your case.

0
source

Source: https://habr.com/ru/post/976503/


All Articles