打印前N个字节?

时间:2019-11-11 20:49:07

标签: r

我编写了一个脚本,该脚本计算了txt文件的字节大小:

byte.counter <- nchar(mytxtfile, type = "bytes")

我的txt的大小约为28894字节,我想将其打印为N字节的块。

1 个答案:

答案 0 :(得分:0)

也许下面的功能是第一步。它将输入文本分成每个length.segm个字符的字符向量。然后,只需打印它们即可。

char.segments <- function(x, segm.length){
  byte.counter <- nchar(x, type = 'bytes')
  f <- c(1, rep(0, segm.length - 1))
  f <- cumsum(rep(f, length.out = byte.counter))
  s <- split(unlist(strsplit(x,'')), f)
  unname(sapply(s, paste, collapse = ''))
}

mytxtfile <- paste(rep(letters, 10), collapse = '')
char.segments(mytxtfile, 26)
# [1] "abcdefghijklmnopqrstuvwxyz" "abcdefghijklmnopqrstuvwxyz" "abcdefghijklmnopqrstuvwxyz"
# [4] "abcdefghijklmnopqrstuvwxyz" "abcdefghijklmnopqrstuvwxyz" "abcdefghijklmnopqrstuvwxyz"
# [7] "abcdefghijklmnopqrstuvwxyz" "abcdefghijklmnopqrstuvwxyz" "abcdefghijklmnopqrstuvwxyz"
#[10] "abcdefghijklmnopqrstuvwxyz"