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

前端 未结 4 1776
花落未央
花落未央 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:31

    As of writing this, fwrite does not support forcing encoding. There is a workaround that I use, but it's a bit more obtuse than I'd like. For your example:

    readr::write_excel_csv(DT[,0],"DT.csv")
    data.table::fwrite(DT,file = "DT.csv",append = T)
    

    The first line will save only the headers of your data table to the CSV, defaulting to UTF-8 with the Byte order mark required to let Excel know that the file is encoded UTF-8. The fwrite statement then uses the append option to add additional lines to the original CSV. This retains the encoding from write_excel_csv, while maximizing the write speed.

提交回复
热议问题