Search code examples
rencodingreadlines

Problem with encoding Readlines(), special character


I have some accents in my file and I'm trying to open this file with the proper encoding, but every time I'm opening this file, my accents come up with special characters (), although I mentioned to open the file with encoding = UTF-8

mes_keywords_to_check <- readLines("mot cle holidays.txt", encoding='UTF-8')

Do you know what is the problem?


Solution

  • I can reproduce your issue:

    temp <- tempfile(fileext = ".txt")
    
    write("Ole Gunnar Solskjær", temp)
    
    readLines(temp, encoding = "UTF-8")
    
    [1] "Ole Gunnar Solskj\xe6r"
    

    However, in my case it is resolved by omitting the encoding parameter in readLines (or using latin1 instead of UTF-8):

    readLines(temp)
    [1] "Ole Gunnar Solskjær"
    
    readLines(temp, encoding = "latin1")
    [1] "Ole Gunnar Solskjær"