I have a file that explicitly says it is UTF-8, the unix command file -i says it is encoded as UTF-8, but when I load it into R (using readr with UTF8 encoding), I still can clearly tell that multi-byte characters are wrong. When I specify "Windows-1252" (which based on this chart, I'm pretty sure is what it was originally encoded as) as the encoding, I get more incorrect characters.
I think what happened is that someone saved these incorrect characters as UTF-8. Is there any way to recover the original text?
Here are attempts at fixing by specifying the encoding:
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}"
Update
Reverse encoding (a la Stat545 example) doesn't work
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}"