R:使用cat()获得类似Unix的换行LF写入文件 [英] R: Getting Unix-like linebreak LF writing files with cat()
问题描述
我尝试在Windows 7/R 3.2.2 x64下将字符向量写入文本文件,并且我想要unix LF-不是Windows CRLF:
I try to write a character vector to a textfile under Windows 7 / R 3.2.2 x64, and I want unix LF - not Windows CRLF:
v <- c("a","b","c")
cat(nl,file="textfile.txt",sep="\n")
写
> a[CRLF]
> b[CRLF]
> c[CRLF]
cat(paste(nl,sep="\n",collapse="\n"),file="t2.txt")
写
> a[CRLF]
> b[CRLF]
> c
我也尝试过write.table(eol ="\ n")-由于似乎在内部使用cat而未成功.
I have also tried write.table(eol="\n") - unsuccessfully as it seems to use cat internally.
我正在寻找其他解决方法; 我试图找到某物.在R \ src \ main \ scan.c中,在387ff行中找到相关代码.
I have looked for other workarounds; I tried to find sth. in R\src\main\scan.c, locating the relevant code in line 387ff.
谁知道我如何在输出文件中获得类似UNIX的LF?
Anyone who knows how I can get UNIX-like LF in my output file?
推荐答案
尝试以二进制"模式(而不是文本"模式)打开文件连接,以防止依赖于行尾的系统编码:
Try to open a file connection in "binary" mode (instead of "text" mode) to prevent system-dependent encoding of the end-of-line:
v <- c("a","b","c")
f <- file("textfile.txt", open="wb")
cat(v,file=f,sep="\n")
close(f)
这篇关于R:使用cat()获得类似Unix的换行LF写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!