代码之家  ›  专栏  ›  技术社区  ›  ebb

R-转换<。出>入<。csv>格式

  •  1
  • ebb  · 技术社区  · 7 年前

    我需要将模型输出文件转换为 .csv 总体安排原始文件位于 .out 格式,和是由多个空格分隔的数据表:

    e、 g.原始文件约有14000行,但结构相同。 请将此链接用于示例文件: https://www.dropbox.com/sh/1w0f9cq2w6gcbjo/AACoGu6L3yKBGBKODY87lygsa?dl=0

    time   dayofyr   nit_N2O-N  dnit_N2O-N   dnit_N2-N        NO-N  CUM-N2O(gN/ha)  CUM-NO(gN/ha)
    1980.00     1      0.3310        0.3403        0.2022        0.0000        0.6713        0.0000  
    1980.00     2      0.3295        0.3400        0.2020        0.0000        1.3408        0.0000  
    1980.00     3      0.3280        0.3397        0.2018        0.0000        2.0086        0.0000  
    1980.00     4      0.3265        0.3395        0.2016        0.0000        2.6746        0.0000  
    1980.00     5      0.3251        0.3393        0.2015        0.0000        3.3389        0.0000  
    1980.00     6      0.3237        0.3391        0.2014        0.0000        4.0017        0.0000  
    1980.00     7      0.3223        0.3389        0.2013        0.0000        4.6629        0.0000  
    1980.00     8      0.3209        0.3387        0.2011        0.0000        5.3225        0.0000  
    1980.00     9      0.3195        0.3386        0.2010        0.0000        5.9805        0.0000  
    1980.00    10      0.3198        0.4589        0.2868        0.0000        6.7592        0.0000  
    

    我已尝试将扩展名更改为。csv和将连续空格转换为“,”,但我恐怕在正则表达式语法或总体原理中缺少了一些东西。

    到目前为止,我得到的是:

    # change extension of all .out files:
    outFiles <- dir(cntPath, "^.+\\.out", full.names = TRUE, ignore.case = TRUE, all.files = TRUE)
    for (nOut in outFiles){
      newOut <- gsub('.out', '.csv', outFiles) # rename files to avoid accidental re-processing
      file.rename(outFiles, newOut)
    }
    # for each file, eliminate initial spaces, and replace consecutive spaces with a comma, for all rows and columns.
    t1 <- read.csv(paste(cntPath, "test2.csv", sep = "/"), header = TRUE, sep = "\t", blank.lines.skip = FALSE)
    for (i in 1:length(nrow(t1))) {
      sub("^\\s+", "^\\S", i)
      sub("\\s+", ",", i)
    }
    

    这应该相对容易,但我不知道哪里出了错。 欢迎提供任何帮助/建议。 谢谢

    2 回复  |  直到 7 年前
        1
  •  1
  •   Sathish    7 年前

    首先,备份所有 .out 文件,以便在出现问题时可以恢复它们。

    备份文件后,请在中执行以下操作 R

    以下代码块将找到 。出来 中指定的目录中的文件 fp ,读它们,写它们为 .csv 并最终删除所有 。出来 文件。如果不想删除文件,请在最后一行添加注释 unlink

    编辑: 习惯于 tryCatch 阻止查找读写失败的文件

    fp <- "."  # specify file path
    fl <- list.files( path = fp, pattern = "*.out", full.names = TRUE ) # get .out files
    for (file in fl ){  # loop through files, read it and write it as .csv files. Then delete .out files
    flag <- tryCatch( {
        write.table( x = read.table(file = file, header = TRUE ), 
                     file = file.path( fp, gsub( "out$", "csv", basename( file ) ) ), 
                     sep = ",", 
                     row.names = FALSE )
        TRUE
        },
        error = function( x ) { 
          print( paste0( "Problem reading and writing file : ", file ) )
          return(FALSE) } )
    
    if( flag ) unlink( file )  # deletes .out files
    }
    
        2
  •  1
  •   Hugh    7 年前

    你的 .out 文件似乎是固定宽度格式,有一个轻微的问题,即名称不对齐。

    没问题,跳过那一行

    library(readr)
    
    fwf_txt <- "time   dayofyr   nit_N2O-N  dnit_N2O-N   dnit_N2-N        NO-N  CUM-N2O(gN/ha)  CUM-NO(gN/ha)
    1980.00     1      0.3310        0.3403        0.2022        0.0000        0.6713        0.0000  
    1980.00     2      0.3295        0.3400        0.2020        0.0000        1.3408        0.0000  
    1980.00     3      0.3280        0.3397        0.2018        0.0000        2.0086        0.0000  
    1980.00     4      0.3265        0.3395        0.2016        0.0000        2.6746        0.0000  
    1980.00     5      0.3251        0.3393        0.2015        0.0000        3.3389        0.0000  
    1980.00     6      0.3237        0.3391        0.2014        0.0000        4.0017        0.0000  
    1980.00     7      0.3223        0.3389        0.2013        0.0000        4.6629        0.0000  
    1980.00     8      0.3209        0.3387        0.2011        0.0000        5.3225        0.0000  
    1980.00     9      0.3195        0.3386        0.2010        0.0000        5.9805        0.0000  
    1980.00    10      0.3198        0.4589        0.2868        0.0000        6.7592        0.0000  "
    
    write_csv(read_fwf(fwf_txt, fwf_empty(fwf_txt, skip = 1),
                       skip = 1),
              path = "fwf.csv",
              col_names = FALSE)
    

    创建日期:2018年3月25日 reprex package (v0.2.0)。