2015-12-29 27 views
1

當我閱讀確定的西班牙語網站時,我在HTML編碼中獲得了西班牙口音。我用readLines函數閱讀網站(我需要使用這個函數)。在R中轉換html西班牙語口音

url <- "http://www.senamhi.gob.pe/include_mapas/_map_data_hist03.php?drEsta=01" 
char_data <- readLines(url,encoding="UTF-8") 

使得所有操作,讓我的數據後,我有一個數據幀在那裏我有能與重音字字符值的變量。它會是這樣的:

var <- rep("Meteorol&oacute;gica",5) 

我需要將西班牙語口音在HTML編碼轉換爲普通西班牙口音。我iconv功能

iconv(var, "UTF-8", "ASCII") 

,但它不能正常工作測試,我得到的輸入相同的字符向量。我還測試了在readLines函數中更改encoding選項,但都不起作用。

我該怎麼辦?謝謝。

+2

更有可能我們將能夠幫助您你做一個最小的可重複的例子來解決你的問題。我們可以從中學習並使用它來向您展示如何解決您的問題。你可以看看[這個SO帖子](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)關於如何在R中創建一個很好的重現例子。 –

回答

0

我不知道R,但如果你能包括它的JavaScript的一條線,這是行:

var encoded = 'H&oacute;la'; 
var notEncoded = encoded.replace("&oacute;", "ò"); 

然後,讓你的.R程序notEncoded值。

+0

謝謝,但代碼不幫我。我知道替換R中的部分文本,但我需要一個特定的函數,因爲我用其他單詞與其他重音元音一起使用。 – pescobar

0

爲什麼不查找所有的HTML &codes;重音字符,然後查找/替換?

library(rvest) 

# scrape lookup table of accented char html codes, from the 2nd table on this page 
ref_url <- 'http://www.w3schools.com/charsets/ref_html_8859.asp' 
char_table <- html(ref_url) %>% html_table %>% `[[`(2) 
# fix names 
names(char_table) <- names(char_table) %>% tolower %>% gsub(' ', '_', .) 

# here's a test string loaded with different html accents 
test_str <- '&Agrave; &Aacute; &Acirc; &Atilde; &Auml; &Aring; &AElig; &Ccedil; &Egrave; &Eacute; &Ecirc; &Euml; &Igrave; &Iacute; &Icirc; &Iuml; &ETH; &Ntilde; &Ograve; &Oacute; &Ocirc; &Otilde; &Ouml; &times; &Oslash; &Ugrave; &Uacute; &Ucirc; &Uuml; &Yacute; &THORN; &szlig; &agrave; &aacute; &acirc; &atilde; &auml; &aring; &aelig; &ccedil; &egrave; &eacute; &ecirc; &euml; &igrave; &iacute; &icirc; &iuml; &eth; &ntilde; &ograve; &oacute; &ocirc; &otilde; &ouml; &divide; &oslash; &ugrave; &uacute; &ucirc; &uuml; &yacute; &thorn; &yuml;' 

# use mgsub from here (it's just gsub with a for loop) 
# http://stackoverflow.com/questions/15253954/replace-multiple-arguments-with-gsub 
mgsub(char_table$entity_name, char_table$character, test_str) 

而且voil &agrave;,有你:

「A A A A A A自動ÇE E E E I I I I d N}÷○○○○×○U U U UÝ ÞßÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃ「

相關問題