R-readLines输出空行 [英] R - readLines outputs empty lines

查看:67
本文介绍了R-readLines输出空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了 readLines()-函数的某些奇怪行为,该行为基本上包含在所有R运行中.当我完成了成千上万次的尝试后,我试图读取文件的以下行:

I have encountered some strange behaviour of the readLines()-function which is basically included for all R-runs. As I have done it houndreds or thousands of times before, I tried to read the lines of a file like this:

workingDir <- ""

tempFile <- file(paste(workingDir, "/stationaer_mittel004_head3D.csv", sep="", collapse=""), open="r")
s_mittel001_head <- readLines(tempFile)
close(tempFile)
s_mittel001_head

这不能正确读取文件内容,并导致如下所示:

This does not read the file content properly and results in something like this:

[1] "\"" ""   ""   ""   ""

如果文件名做错了,我已经进行了三重和三重和四重检查,但是当在文件浏览器或Web浏览器中访问文件时,它工作得很好.随后,我将文件内容清楚地复制到了另一个csv,并试图打开它.确实有效.我使用笔记本电脑的管理帐户,两个文件均未显示任何特殊权限限制.用相同的文件名创建一个新文件也可以解决该问题.

I have double- and tripöle and quadruple-checked if I have done something wrong with the filename but it works just fine when accessing the file in a file- or webbrowser. Subsequently, I plainly copied the file content to another csv and tried to open this one. It actually worked. I am on the administrive account of my laptop and both files show no special permission restrictions. Creating a new file with the same filename solves the issue, too.

**这就是为什么我想知道是否有人知道可能导致这种行为的原因.

**That's why I would like to know if somebody knows what might cause this behaviour.

谢谢!**

推荐答案

正如@CarlWitthoft指出的那样, readLines()在读取具有某些换行符类型的ASCII文件时遇到问题(不知道哪个恰恰是:vs vs).这将产生一个实际上是空字符串的列表.此外,该功能似乎无法看到文件结尾.

As @CarlWitthoft pointed out, readLines() has problems reading ASCII-files with certain linebreak-types (don't know which exactly: vs vs ). This results in a list of practically empty strings. Furthermore, the function seemingly is unable to see the end of the file.

最终解决方案:使用符合更适当"换行标准的程序将文件内容复制并粘贴到新文件中.

The final solution: Copy and paste the file content in a new file using a program with "more appropiate" linebreak-standard.

这篇关于R-readLines输出空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆