简体   繁体   English

读取多个SRT文件

[英]Reading in multiple srt files

I'd like to read in multiple srt files in R. I can read them into a list but I need to load them in sequentially by the way they were created in the file directory. 我想读入R中的多个srt文件。我可以将它们读入列表,但是我需要按照它们在文件目录中创建的方式依次加载它们。

I'd also like to make a column to tell which file they come from. 我也想在专栏中说一下它们来自哪个文件。 So I can tell which data came from file 1, file 2.. etc. 所以我可以分辨出哪些数据来自文件1,文件2等。

I can read them in as a list; 我可以阅读它们作为列表。 but the files are names like "1 - FileTest"; 但是文件的名称类似于“ 1- FileTest”; "2 - FileTest", "#10 FileTest",... etc “ 2-FileTest”,“#10 FileTest”等...

This then loads the list like 1, 10, 11... etc. Even though if I arrange the files in my file directory file 11 was created after 9 for instance. 然后,它将像1、10、11 ...等那样加载列表。即使我将文件排列在文件目录中,文件11也是在9之后创建的。 I should just need a parameter for them to load sequentially so then when I put them in dataframe they show in chronological order. 我只需要一个参数即可按顺序加载它们,因此当我将它们放入数据框中时,它们将按时间顺序显示。

list_of_files <- list.files(path=path,  
                        pattern = "*.srt", 
                        full.names = TRUE)

Files <- lapply(list_of_files, srt.read)

Files  <- data.frame(matrix(unlist(Files),  byrow=T),stringsAsFactors=FALSE)

The files load in but they don't load in chronological order it is difficult to tell what data is associated with which file. 文件已加载,但它们没有按时间顺序加载,因此很难确定哪些数据与哪个文件相关联。

I have approximately 150 files so being able to compile them into a single dataframe would be very helpful. 我大约有150个文件,因此能够将它们编译成一个数据框将非常有帮助。 Thanks! 谢谢!

Consider extracting meta data of the files with file.info (includes created/modified time, file size, owner, group, etc.). 考虑使用file.info提取文件的元数据(包括创建/修改的时间,文件大小,所有者,组等)。 Then order that resulting data frame by created date/time, and finally import .srt files with ordered list of files: 然后按创建的日期/时间对结果数据框进行order ,最后导入具有文件列表的.srt文件:

raw_list_of_files <- list.files(path=path,  
                                pattern = "*.srt", 
                                full.names = TRUE)

# CREATE DATA FRAME OF FILE INFO
meta_df <- file.info(raw_list_of_files)

# SORT BY CREATED DATE/TIME
meta_df <- with(meta_df, meta_df[order(ctime),])

# IMPORT DATA FRAMES IN ORDERED FILES
srt_list <- lapply(row.names(meta_df), srt.read)

final_df  <- data.frame(matrix(unlist(srt_list),  byrow=TRUE),
                        stringsAsFactors=FALSE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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