Jer*_*oen 6 r character-encoding
有没有办法获得(和设置)encoding
现有连接?例如:
con <- file(tempfile(), encoding = "UTF-8")
summary(con)
Run Code Online (Sandbox Code Playgroud)
摘要列出了模式以及是否已打开,但不列出连接使用的编码.
我真的不确定我是否理解你需要做什么。但假设
如果你要强制使用 UTF-8 编码,你可以这样做:
# 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"
Run Code Online (Sandbox Code Playgroud)
检查以前的内容是否使用 UTF-8 编码完全是另一回事,因此这对于您的情况可能有用,也可能没用。