How can i specify encode in fwrite() for export csv file R?

前端 未结 4 1750
花落未央
花落未央 2021-01-13 18:19

Since fwrite() cannot apply encoding argument , how can i export csv file in specific encode as fast as fwrite() ? (fwrite()

4条回答
  •  离开以前
    2021-01-13 18:55

    You should post a reproducible example, but I would guess you could do this by making sure the data in DT is in UTF-8 within R, then setting the encoding of each column to "unknown". R will then assume the data is encoded in the native encoding when you write it out.

    For example,

    DF <- data.frame(text = "á", stringsAsFactors = FALSE)
    DF$text <- enc2utf8(DF$text) # Only necessary if Encoding(DF$text) isn't "UTF-8"
    Encoding(DF$text) <- "unknown"
    data.table::fwrite(DF, "DF.csv", bom = TRUE)
    

    If the columns of DF are factors, you'll need to convert them to character vectors before this will work.

提交回复
热议问题