Created
March 17, 2016 22:17
-
-
Save mattpolicastro/fd1088ceb6a2aeef5dcc to your computer and use it in GitHub Desktop.
A quick-and-dirty .rpt-to-.csv file converter. No safety for escapes. ¯\_(ツ)_/¯
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
text <- readLines("file.rpt") | |
col_lengths <- unlist(lapply(strsplit(text[2], " "), nchar)) | |
new_cols <- list() | |
for(col in 1:length(col_lengths)) { | |
current_len <- col_lengths[col] | |
new_cols[[col]] <- as.character(gsub("^\\s+|\\s+$", "", strtrim(text, current_len))) | |
text <- substring(text, current_len + 2) | |
} | |
remove(col) | |
data <- as.data.frame(new_cols, stringsAsFactors = FALSE) | |
names(data) <- unlist(data[1,]) | |
data <- data[3:nrow(data),] | |
write.csv(data, file = "file.rpt.csv", row.names = F) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment