Home > Software design >  Detect plain text file encoding
Detect plain text file encoding

Time:11-20

I am working with text files from which I load the text content to send through API (as the body of a PUT request). Typically, I use readLines()

my_text_content <- readLines(con = "path\to\file.txt")

The distant app has problem to deal with non UTF-8 text encoding. And it appears that some users in Windows, using Notepad to write their scripts, dont default to UTF-8 but most often ANSI.

I found on other questions how to convert encoding using iconv(), but I would first like to detect file encoding to raise a warning to the user (and eventually convert encoding then).

Thank you for your help !

CodePudding user response:

You can try this:

library(readr)

guess_encoding("text.txt")

# A tibble: 1 x 2
  encoding confidence
  <chr>         <dbl>
1 ASCII             1
  • Related