如何使用R解析INI配置文件?

时间:2014-01-11 16:26:07

标签: r ini

是否有用于解析INI like configuration files的R函数?

搜索时我才发现this discussion

1 个答案:

答案 0 :(得分:2)

以下是2007年r-help上same question的确切答案(感谢@Spacedman指出这一点):

Parse.INI <- function(INI.filename) 
{ 
  connection <- file(INI.filename) 
  Lines  <- readLines(connection) 
  close(connection) 

  Lines <- chartr("[]", "==", Lines)  # change section headers 

  connection <- textConnection(Lines) 
  d <- read.table(connection, as.is = TRUE, sep = "=", fill = TRUE) 
  close(connection) 

  L <- d$V1 == ""                    # location of section breaks 
  d <- subset(transform(d, V3 = V2[which(L)[cumsum(L)]])[1:3], 
                           V1 != "") 

  ToParse  <- paste("INI.list$", d$V3, "$",  d$V1, " <- '", 
                    d$V2, "'", sep="") 

  INI.list <- list() 
  eval(parse(text=ToParse)) 

  return(INI.list) 
} 

实际上,我写了一个简短且大概有错误的功能(即不覆盖所有角落的情况),现在对我有用:

read.ini <- function(x) {
    if(length(x)==1 && !any(grepl("\\n", x))) lines <- readLines(x) else lines <- x
    lines <- strsplit(lines, "\n", fixed=TRUE)[[1]]
    lines <- lines[!grepl("^;", lines) & nchar(lines) >= 2]  # strip comments & blank lines
    lines <- gsub("\\r$", "", lines)
    idx <- which(grepl("^\\[.+\\]$", lines))
    if(idx[[1]] != 1) stop("invalid INI file. Must start with a section.")

    res <- list()
    fun <- function(from, to) {
        tups <- strsplit(lines[(from+1):(to-1)], "[ ]*=[ ]*")
        for (i in 1:length(tups)) 
            if(length(tups[[i]])>2) tups[[i]] <- c(tups[[i]][[1]], gsub("\\=", "=", paste(tail(tups[[i]],-1), collapse="=")))
        tups <- unlist(tups)
        keys <- strcap(tups[seq(from=1, by=2, length.out=length(tups)/2)])
        vals <- tups[seq(from=2, by=2, length.out=length(tups)/2)]
        sec <- strcap(substring(lines[[from]], 2, nchar(lines[[from]])-1))
        res[[sec]] <<- setNames(vals, keys)
    }
    mapply(fun, idx, c(tail(idx, -1), length(lines)+1))
    return(res)
}

其中strcap是一个大写字符串的辅助函数:

strcap <- function(s) paste(toupper(substr(s,1,1)), tolower(substring(s,2)), sep="")

还有一些C解决方案,例如inihlibini可能有用。不过,我没试过。