当前位置: 动力学知识库 > 问答 > 编程问答 >

perl - Parsing CSV files from multiple folders

问题描述:

I have a folder that contains multiple sub folders and each sub folder contains different types of files and sometimes contains a file called

run.csv. However the CSV file is always named run.csv and consist always the columns id, lane, index and others.

I know how to parse CSV files but couldnt figure out how to loop through the subfolders and print all the id, lane and index columns without the header information from all CSV files at the end. For this I should use ideally the perl language.

网友答案:

I think what you want is glob:

foreach my $csv_file ( glob "/path/to/dir/*/run.csv" ) {
    #process CSV. 
}

But it's possible you want File::Find::Rule:

foreach my $file ( File::Find::Rule -> name ('run.csv') -> in ( "/path/to/dir" ) ) {
   print $file,"\n";
}

Which does much the same, but recursively. In both cases, you get a relative path to the current directory, so can just pass it straight to open.

分享给朋友:
您可能感兴趣的文章:
随机阅读: