Convert and export unicode to R

I created the script below to convert Unicode to Chinese characters, the last line in temp.df[,"name_unicode"]is “§® £" (without a quote) so people who don’t know Chinese can also help.

library(RODBC)
library(Unicode)

temp.df <- data.frame(name_unicode=c("&#38515;&#22823;&#25991;",
                                     "&#38515;&#23567;&#25935;",
                                     "&#38515;&#19968;&#23665;",
                                     "&#167;&#174;&#163;"),
                      stringsAsFactors=FALSE)

temp.df[,"name_unicode_mod"] <- sapply(temp.df[,"name_unicode"],
                                        function(x) {
                                          temp <- unlist(strsplit(x,";"))
                                          temp <- sprintf("%x",as.integer(gsub("[^0-9]","",temp)))
                                          temp <- intToUtf8(as.u_char_range(temp))
                                          return(temp)
                                          })


write.csv(temp.df,file("test.csv",encoding="UTF-8"),row.names=FALSE)

The output for temp.df[,"name_unicode_mod"]is suitable for console R. But I need to export them in the csvor format xls. I tried to write.csv, write.table, odbcConnectExcelat RODBC, but still gives me something like <U+00A7><U+00AE><U+00A3>.

Can anyone help? Thank.

PS I am using R 3.0.0 and Win7

+5
source share
1 answer

Using binary writing will work for your business. Below is a small sample code.

writeUtf8csv <- function(x, file) {
  con <- file(file, "wb")
  apply(x, 1, function(a) {
      b <- paste(paste(a, collapse=','), '\r\n', sep='')
      writeBin(charToRaw(b), con, endian="little")
    })
  close(con)
}

.

+5

All Articles