Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.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
在python/R中将utf8解码为常规字符_Python_R_Encoding_Utf - Fatal编程技术网

在python/R中将utf8解码为常规字符

在python/R中将utf8解码为常规字符,python,r,encoding,utf,Python,R,Encoding,Utf,我有各种各样的字符串,例如xc3\x93\xc5\x81,它们是编码的UTF-8字符。我能访问的唯一文件是那些编码值。如何在R或python中将其解码为常规字符(不是这个UTF-8俚语)?在R中,我们可以使用@Jeroen的函数at,只需稍加修改即可处理\xnn而不是\unnn unescape_unicode <- function(x){ #single string only stopifnot(is.character(x) && length(x) ==

我有各种各样的字符串,例如
xc3\x93\xc5\x81
,它们是编码的UTF-8字符。我能访问的唯一文件是那些编码值。如何在R或python中将其解码为常规字符(不是这个UTF-8俚语)?

在R中,我们可以使用@Jeroen的函数at,只需稍加修改即可处理
\xnn
而不是
\unnn

unescape_unicode <- function(x){
  #single string only
  stopifnot(is.character(x) && length(x) == 1)

  #find matches
  m <- gregexpr("(\\\\)+x[0-9a-z]{2}", x, ignore.case = TRUE)

  if(m[[1]][1] > -1){
    #parse matches
    p <- vapply(regmatches(x, m)[[1]], function(txt){
      gsub("\\", "\\\\", parse(text=paste0('"', txt, '"'))[[1]], fixed = TRUE, useBytes = TRUE)
    }, character(1), USE.NAMES = FALSE)

    #substitute parsed into original
    regmatches(x, m) <- list(p)
  }

  x
}

由(v0.2.1)创建于2019-04-15。请尝试使用字符串的decode()方法。在R中,请尝试
stringi::stri\u unescape\u unicode