Search code examples
rencodingutf-8readlinesiso-8859-2

readlines files in r with differents encoding


I have :

guess_encoding("file.txt")$encoding
[1] "UTF-8"      "ISO-8859-1" "ISO-8859-2"

I want to read my file :

lines <- readLines("file.txt", encoding = guess_encoding("file.txt")$encoding)

but it return this error

Error in readLines("file.txt", encoding = guess_encoding("file.txt")$encoding) : 
  invalid 'encoding' value

Solution

  • you showed that guess_encoding (from readr) made 3 guesses

    "UTF-8" "ISO-8859-1" "ISO-8859-2"
    

    to pick the first guess and try to use it would be

    
     lines <- readLines("file.txt", encoding = guess_encoding("file.txt")$encoding[[1]])