欢迎投稿

今日深度:

R 中的UNICODE字符转换为中文,

R 中的UNICODE字符转换为中文,


Well, first understand that certain character in R must be escaped if they are outside the standard ASCII-characters. Typically this is done with a "\" character. That's why you need to escape this character when you write a string in R
a <- "\" # error
a <- "\\" # ok.


The "\U" is a special indicator for unicode escaping. Note that there are no slashes or U's in the string itself when you use this escaping. It is just a shortcut to a specific character. Note:

a <- "\U00B5"
cat(a)
# µ
grep("U",a)
# integer(0)
nchar(a)
# [1] 1

This is very different than the string
a <- "\\U00B5"
cat(a)
# \U00B5
grep("U",a)
# [1] 1
nchar(a)
# [1] 6

Normally when you import text file, you would encode non-ASCII character in whatever encoding is used by the file (UTF-8, or Latin-1 are the most common). They have special bytes to represent these characters. It's not "normal" for a text file to have an ASCII escape sequence for unicode characters. This is why R doesn't attempt to convert "\U00B5" to a unicode character because it assumes that if you had wanted a unicode character, you would have just used it directly. 

The easiest way to re-interpet your ASCII character values would be to use the stringi package. For exampel
library(stringi)
a <- "\\U00B5"
stri_unescape_unicode(gsub("\\U","\\u",a, fixed=TRUE))
(the only catch is that we needed to convert "\U" to the more common "\u" so the function properly recognized the escape). You can do this to your imported data with
test$label <- stri_unescape_unicode(gsub("\\U","\\u",test$label, fixed=TRUE))


---------------------------------------------------------------------------------------------------------------------------------------------

library(stringi)
x="\u5e26\u6709\u6807\u7b7e";x  # "带有标签"


cat("a\u0105!")  # aą!
stri_escape_unicode("a\u0105!")  #  "a\\u0105!"
stri_unescape_unicode("a\u0105!")  # "aą!"

getOption("encoding")
options("endocing"="UTF-8")

setwd("C:\\My Documents\\")

movie_url=paste("https://api.douban.com/v2/movie/search?tag=喜剧&start=200")

## Readl URL
raw.data=readLines(movie_url, encoding="UTF-8");raw.data

writeLines(stri_unescape_unicode(raw.data), "Test-1.txt", useBytes=T) # Export to file.

## JSON 
x='{"count": 20, "start": 200, "total": 200, "Name":"Sam"}'
xx=fromJSON(x)
is.list(xx)
str(xx)
summary(xx)


www.htsjk.Com true http://www.htsjk.com/teradata/37110.html NewsArticle R 中的UNICODE字符转换为中文, Well, first understand that certain character in R must be escaped if they are outside the standard ASCII-characters. Typically this is done with a "\" character. That's why you need to escape this ch...
相关文章
    暂无相关文章
评论暂时关闭