R编码 - 保存为带有错误字符的UTF-8(我认为)

时间:2017-08-08 15:10:20

标签: r encoding utf-8 character-encoding

我有一个明确表示它是UTF-8的文件,unix命令file -i表示它被编码为UTF-8,但当我将它加载到R(使用带有UTF8编码的读取器)时,我仍然可以清楚地看出多字节字符是错误的。当我指定“Windows-1252”(基于this chart,我很确定它最初被编码为)作为编码时,我得到更多不正确的字符。

我认为发生的事情是有人将这些不正确的字符保存为UTF-8。有没有办法恢复原始文本?

以下是通过指定编码进行修复的尝试:

library(curl)
library(readr)
#> 
#> Attaching package: 'readr'
#> The following object is masked from 'package:curl':
#> 
#>     parse_date

text_file <- tempfile()
curl_download("https://dl.dropboxusercontent.com/s/7syikmmiduubsqv/test.txt", text_file)


# Default is UTF-8, other specifications add extra characters
read_lines(text_file)
#> [1] "{Província}"
# read_lines(text_file, locale = locale(encoding = "UTF-8")) # same
read_lines(text_file, locale = locale(encoding = "Windows-1252"))
#> [1] "{ProvÃ<U+0083>­ncia}"
read_lines(text_file, locale = locale(encoding = "latin1"))
#> [1] "{ProvÃ<U+0083>­ncia}"

# Same as equivalent readr code
# readLines(text_file)
# readLines(text_file, encoding = "UTF-8")
# readLines(text_file, encoding = "UTF-8-BOM")
# readLines(text_file, encoding = "Windows-1252")

# Desired text: "{Prov\u00EDncia}"

更新

反向编码(la Stat545 example)不起作用

iconv(read_lines(text_file), from = "UTF-8", to = "Latin1")
#> [1] "{Província}"
iconv(read_lines(text_file), from = "UTF-8", to = "Windows-1252")
#> [1] "{Província}"

2 个答案:

答案 0 :(得分:1)

由于声誉低下,我无法发表评论,但是您的职能帮助了我。我发布的原因是该函数中存在一些错误(括号错误和actual_chars_current未定义)。

编辑:

 create_utf_crosswalk <- function() {
    # Affects Windows-1252 0x80 - 0xFF (but a few characters aren't in
    # the spec, so  remove them)
    hex_codes <- sprintf("%x", seq(strtoi("0x80"), strtoi("0xFF")))
    hex_codes <- hex_codes[!hex_codes %in% c("81", "8d", "8f", "90", "9f")]

    actual_chars_locale <- vapply(hex_codes, FUN.VALUE = character(1), function(x) {
      parse(text = paste0("'\\x", x, "'"))[[1]]
    })

    actual_chars_utf <- iconv(actual_chars_locale, to = "UTF-8")

    mangled_chars_utf <- vapply(actual_chars_utf, FUN.VALUE = character(1), 
                                function(x){
                                  Encoding(x) <- "Windows-1252"
                                  x
                                })

    out <- actual_chars_utf
    names(out) <- mangled_chars_utf
    out
  }

答案 1 :(得分:0)

好吧,我想有一个更好的方法可以解决这个问题,但在有人发布之前,这里有一个解决方案,可以从网站创建表格并用文字替换它。

(需要使用stringr)


# Create the Debugging table from http://www.i18nqa.com/debug/utf8-debug.html
# UTF-8 characters were interpreted as Windows-1252 and then saved
# as UTF-8
create_utf_crosswalk <- function() {
  # Affects Windows-1252 0x80 - 0xFF (but a few characters aren't in
  # the spec, so  remove them)
  hex_codes <- sprintf("%x", seq(strtoi("0x80"), strtoi("0xFF")))
  hex_codes <- hex_codes[!hex_codes %in% c("81", "8d", "8f", "90", "9f")]

  actual_chars_locale <- vapply(hex_codes, FUN.VALUE = character(1), function(x) {
    parse(text = paste0("'\\x", x, "'"))[[1]]
  })

  actual_chars_utf <- iconv(actual_chars_current, to = "UTF-8")

  mangled_chars_utf <- vapply(actual_chars_utf, FUN.VALUE = character(1), 
  function(
    Encoding(x) <- "Windows-1252"
    x
  })

  out <- actual_chars_utf
  names(out) <- mangled_chars_utf
  out
}

text_file <- tempfile()
curl::curl_download("https://dl.dropboxusercontent.com/s/7syikmmiduubsqv/test.txt", text_file)
test_text <- readr::read_lines(text_file)

utf_fix <- create_utf_crosswalk()

stringr::str_replace_all(test_text, utf_fix)
#> [1] "{Província}"

更新

找出一个直接解决方案,它适用于示例文本,但不适用于完整文件(可能我没有指定完全正确的文件编码)。

text <- readLines("https://dl.dropboxusercontent.com/s/7syikmmiduubsqv/test.txt")

fixed <- iconv(text, from = "UTF-8", to = "Windows-1252")
Encoding(fixed) <- "UTF-8"

fixed