簡體   English   中英

Windows和Mac上的UTF-8編碼使字符混亂

[英]UTF-8 encoding on Windows and Mac messes characters

我想使用Windows(7 64位)計算機從Itunes API獲取數據並在Mac(64位El Capitan)上處理該數據。 我正在使用RJSONIO包提取應用程序的名稱,它們來自不同國家的不同語言。 我僅附帶了一些僅包含少數應用程序的示例。 我首選的編碼是UTF-8。

library(RJSONIO)

getall<-function(ID){
u<-ID
lapply(X = u, function(u){
    dat <- fromJSON(u, encoding = "UTF-8")
    Name<-try(dat$results[[1]]$trackName)
    Artistname<-try(dat$results[[1]]$artistName)
    Seller<-try(dat$results[[1]]$sellerName)
    results<-return(list(Name, Artistname,Seller))
    })
}

apps1<-c("https://itunes.apple.com/lookup?id=335549244", "https://itunes.apple.com/lookup?id=362032276", "https://itunes.apple.com/lookup?id=353410020", "https://itunes.apple.com/lookup?id=350146139","https://itunes.apple.com/lookup?id=358942449", "https://itunes.apple.com/lookup?id=359871187")
    system.time(itunesNew<-data.frame(matrix(unlist(getall(ID = apps1), use.names = FALSE), nrow = length(apps1), ncol = 3, byrow = TRUE),stringsAsFactors=FALSE, byrow=T))
    colnames(itunesNew)<-c("Name", "Artistname","Seller")
    itunesnew2<-cbind(apps1, itunesNew)

我將R和R Studio(均為最新版本)一起使用,並在全局選項中將標准編碼設置為UTF-8。 我無法使用以下方式將語言環境設置為UTF-8:

Sys.setlocale("LC_MESSAGES", 'en_GB.UTF-8')

或R中的其他版本。我還嘗試在“ latin1”中下載數據(在PC上看起來還不錯),但在Mac上卻搞砸了(在R Studio中將編碼設置為latin1)。

問題

  1. 有沒有辦法使用UTF-8在兩台計算機上處​​理數據?
  2. 在這兩台機器上還有其他選擇可以使用嗎?
  3. 更籠統:對於這樣的數據,UTF-8是否應該首選編碼?

我沒有Windows VM,但可以嘗試一下(它在兩個系統上都使用jsonlitedplyr來查看是否有幫助(我在OS X上運行了它):

library(jsonlite)
library(dplyr)

"%||%" <- function(a, b) { if (!is.null(a)) a else b }

apps <- c("https://itunes.apple.com/lookup?id=335549244", 
          "https://itunes.apple.com/lookup?id=362032276", 
          "https://itunes.apple.com/lookup?id=353410020", 
          "https://itunes.apple.com/lookup?id=350146139",
          "https://itunes.apple.com/lookup?id=358942449", 
          "https://itunes.apple.com/lookup?id=359871187")

bind_rows(lapply(apps, function(x) {
  res <- jsonlite::fromJSON(x, flatten=TRUE)$results
  data_frame(name=res$trackName %||% NA,
             artist_name=res$sellerName %||% NA,
             seller=res$sellerName %||% NA)
})) -> dat

glimpse(dat)

## Observations: 6
## Variables: 3
## $ name        (chr) "A+ the Waverley Novels Collection (15Books)", "A+ 中國養生寶典[卷一]", "...
## $ artist_name (chr) "rice mi", "CHEUNG PUI MAN", "CHEUNG PUI MAN", "CHEUNG PUI MAN", ...
## $ seller      (chr) "rice mi", "CHEUNG PUI MAN", "CHEUNG PUI MAN", "CHEUNG PUI MAN", ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM