简体   繁体   中英

R: How to read text file into a lists of lists recursively?

I have a text file in the format:

date=1638.1.16
player=\"BYZ\"
savegame_version={
\tfirst=1
\tsecond=25
\tthird=1
\tforth=0
\tname=\"England\"
}
mod_enabled={
\t\"Large Font\"
\t\"Large Tooltips\"
}

What I want to do is read this into R as a list of character vectors where the { and } symbol indicate the creation of another list. The result should look like:

[[1]]
[1] "date=1638.1.16"

[[2]]
[1] "player=\"BYZ\""

[[3]]
[[3]][[1]]
[1] "savegame_version={"

[[3]][[2]]
[1] "\tfirst=1"

[[3]][[3]]
[1] "\tsecond=25"

[[3]][[4]]
[1] "\tthird=1"

[[3]][[5]]
[1] "\tforth=0"

[[3]][[6]]
[1] "\tname=\"England\""

[[3]][[7]]
[1] "}"

[[4]]
[[4]][[1]]
[1] "mod_enabled={"

[[4]][[2]]
[1] "\t\"Large Font\""

[[4]][[3]]
[1] "\t\"Large Tooltips\""

[[4]][[4]]
[1] "}"

I've tried iterating through the rows of the data with a function that creates lists, where the { symbol recursively calls on the same function again. The issue is that the result is just one list, not a nested one as seen above.

The current function written as:

list_create <- function(vector){
  temp_list <- list()
  for(i in 1:length(vector)){
    if(str_detect(vector[i], pattern = "\\{")) {
      list_create(vector[i+1:length(vector)])
    }
    if(str_detect(vector[i], pattern = "\\}")) {
      return(temp_list)
    }
    temp_list <- append(temp_list, vector[i])
  }
}

Is there any way to get the result I want?

How many layers of sub-lists do you have? For the example you provided (with only 2 levels of lists) this should work:

# read the file in
txt <- readLines("listtext.txt")

# create an empty list
main.list <- list()

# indicator that we are within sublist
sub=FALSE

# loop through each line
for( i in seq(txt) ){

  # check if the string opens a new sublist
  if ( grepl("\\{", txt[i]) ){
    sub.list <- list()   # start a new sublist
    sub.list <- c(sub.list, txt[i])  # add the line as the first line in the new list
    sub = TRUE                       # inside the sublist

  # check if we need to close sublist
  } else if(grepl("\\}", txt[i]) ){
    sub.list <- c(sub.list, txt[i])  # add the last line to sublist
    main.list <- c(main.list, list(sub.list))   # add sublist to the main list
    sub=FALSE                        # no longer inside sublist

  # if we are within sublist    
  } else if(sub) {
    sub.list <- c(sub.list, txt[i])

  # regular record    
  } else {
    main.list <- c(main.list, txt[i] )
  }
}

main.list
# [[1]]
# [1] "date=1638.1.16"
# 
# [[2]]
# [1] "player=\\\"BYZ\\\""
# 
# [[3]]
# [[3]][[1]]
# [1] "savegame_version={"
# 
# [[3]][[2]]
# [1] "\\tfirst=1"
# 
# [[3]][[3]]
# [1] "\\tsecond=25"
# 
# [[3]][[4]]
# [1] "\\tthird=1"
# 
# [[3]][[5]]
# [1] "\\tforth=0"
# 
# [[3]][[6]]
# [1] "\\tname=\\\"England\\\""
# 
# [[3]][[7]]
# [1] "}"
# 
# 
# [[4]]
# [[4]][[1]]
# [1] "mod_enabled={"
# 
# [[4]][[2]]
# [1] "\\t\\\"Large Font\\\""
# 
# [[4]][[3]]
# [1] "\\t\\\"Large Tooltips\\\""
# 
# [[4]][[4]]
# [1] "}"

If you have many recursive sub-lists, than you can write a recursive function:

main.list <- list()
subfun <- function(istart, txt){

  sub.list <- list()
  sub.list <- c(sub.list, txt[istart])
  j = istart + 1
  while( !grepl("\\}", txt[j]) ){

    if ( grepl("\\{", txt[j]) ){
      x <- subfun(j, txt)
      sub.list <- c(sub.list, list(x$sub) )  # add sublist to the main list
      j=x$iend

      # regular record    
    } else {
      sub.list <- c(sub.list, txt[j] )
    }    
    j <- j+1
  }
  sub.list <- c(sub.list, txt[j])
  return(list(sub=sub.list, iend=j))
}

# loop through each line
i=1
while( i <= length(txt) ){

  # check if the string opens a new sublist
  if ( grepl("\\{", txt[i]) ){
    x <- subfun(i, txt)
    main.list <- c(main.list, list(x$sub) )  # add sublist to the main list
    i=x$iend

    # regular record    
  } else {
    main.list <- c(main.list, txt[i] )
  }
  i <- i+1
}

For your example it will produce the same result as the first approach

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM