我在读取包含异常字符的文件时遇到问题,在本例中为箭头符号: .已尝试指定输入文件格式,例如:
> scan('SMKA121212' , what="", sep="\n", blank.lines.skip=T, fileEncoding="UTF-8")
Read 13 items
Warning message:
In scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
invalid input found on input connection 'SMKA121212'
> scan('SMKA121212', what="", sep="\n", blank.lines.skip=FALSE, encoding="UTF-8")
Read 1724 items
(实际上有超过 10k 行并且读取在箭头字符上中断)
我有点不清楚
encoding
之间的区别和 fileEncoding
在 R 如何响应它不期望的字符方面。澄清可能有用。感谢有关如何强制 R 阅读完整文档的任何建议,并且可能只是忽略不符合系统的字符。
最佳答案
我在文本编辑器中看到的是“|”的使用作为分隔符而不是 "\n"并且在第 1724 行这个序列:
kibbled [ìGrtzeî or ìgruttenî], pearl...
有两个不同的重音字符似乎包围了
Grtze
和 grutten
但是你看到的字符没有显示出来。当我在 Mac 上阅读它时:
read.table("~/Downloads/lines/1720-1730.txt", sep="|")
有问题的字符是这样出现的:
[\x93Gr\032tze\x94 or \x93grutten\x94]
所以你看到的“箭头”是
\032
.我发现很难破译各种“转义”R 输出的含义。最好看的地方是?Quotes
页面,我们了解到这是 32 八进制或 26 十进制。您可能想在输入策略时尝试一下,看看它是如何进行的:x <- read.table("yourpath/filename.txt", sep="|", stringsAsFactors=FALSE, allowEscapes = TRUE)
如果这还不够,请尝试添加编码选项“latin1”、“UTF-8”、“UTF-16”之一,如果不成功,还有其他 Windows 编码尚未尝试。
当您收到有关元素数量较少的消息时,通常意味着存在不匹配的引号或嵌入的哈希(“#”)。您可以添加以下参数:
quote="", comment.char=""
.如果您想查看这些附加注释的效果,可以使用以下命令: table(count.fields("yourpath/filename.txt", sep="|", stringsAsFactors=FALSE,
allowEscapes = TRUE, quote="", comment.char=""))
还有进一步的检查操作可以让您查看哪些线路有问题:
which(count.fields("yourpath/filename.txt", sep="|", stringsAsFactors=FALSE,
allowEscapes = TRUE, quote="", comment.char="") == 28)
您的语言环境和默认编码之间可能不匹配。你应该报告
sessionInfo()
的结果我看到提到的用于解决奇怪问题的编码包括“CP1252”、“Latin2”(即 ISO-8859-2),但我发现编码列表比我预期的要大:
iconvlist() # 419 encodings
如果您知道创建该文件的组织,那么为什么不问问他们呢?
从包含在该“主”zip 文件中的多个 zip 文件中的第一个,我们看到了我建议使用
count.fields
的结果。 :table( count.fields("~/Downloads/SMKA12_2012archive/SMKA121212", quote="",
sep="|",comment.char="") )
#------------
15 27 28
1 10228 1
which( count.fields("~/Downloads/SMKA12_2012archive/SMKA121212", quote="", sep="|",comment.char="") ==15)
#[1] 1
which( count.fields("~/Downloads/SMKA12_2012archive/SMKA121212", quote="", sep="|",comment.char="") ==28)
#[1] 10230
在装有 R 3.0.1 和 TextEdit.app 的 Mac 上阅读这些文件。第一个记录似乎不是真正的标题,而是一个符号,可能表示数据记录的月份:
000000000|||||||||||||||||||||||||HMCUSTOMS CONTROL DATA|2012|12
最后一条记录有一个非数据尾随记录,其中包括附加的最终记录计数。
999999999| | | | | | | | | | | | | | | | | | | | | | | | | | |0010228
因此,使用 skip= 1 和 fill =TRUE 应该允许无错误输入。
dat <- read.table("~/Downloads/SMKA12_2012archive/SMKA121212", quote="", sep="|",comment.char="", fill=TRUE, skip=1 , colClasses=c( rep("integer", 2), rep("character", 4), rep("integer", 24-7+1), rep("character", 3)))
> str(dat)
'data.frame': 10230 obs. of 27 variables:
$ V1 : int 10110100 10110900 10121000 10129100 10129900 10130000 10190000 10190110 10190190 10190300 ...
$ V2 : int 0 0 0 0 0 0 0 0 0 0 ...
$ V3 : chr "00/00" "00/00" "01/12" "01/12" ...
$ V4 : chr "12/11" "12/11" "00/00" "00/00" ...
$ V5 : chr "00/00" "00/00" "01/12" "01/12" ...
$ V6 : chr "12/11" "12/11" "00/00" "00/00" ...
$ V7 : int 0 0 0 0 0 0 0 0 0 0 ...
$ V8 : int 150 150 150 150 150 150 150 150 150 150 ...
$ V9 : int 2 2 2 2 2 2 2 2 2 2 ...
$ V10: int 13 13 13 13 13 13 13 13 13 13 ...
$ V11: int 0 0 0 0 0 0 0 0 0 0 ...
$ V12: int 200 200 200 200 200 200 200 200 200 200 ...
$ V13: int 0 0 0 0 0 0 0 0 0 0 ...
$ V14: int 0 0 0 0 0 0 0 0 0 0 ...
$ V15: int 0 0 0 0 0 0 0 0 0 0 ...
$ V16: int 0 0 0 0 0 0 0 0 0 0 ...
$ V17: int 0 0 0 0 0 0 0 0 0 0 ...
$ V18: int 0 0 0 0 0 0 0 0 0 0 ...
$ V19: int 0 0 0 0 0 0 0 0 0 0 ...
$ V20: int 0 0 0 0 0 0 0 0 0 0 ...
$ V21: int 0 0 0 0 0 0 0 0 0 0 ...
$ V22: int 0 0 0 0 0 0 0 0 0 0 ...
$ V23: int 0 0 0 0 0 0 0 0 0 0 ...
$ V24: int 0 0 0 0 0 0 0 0 0 0 ...
$ V25: chr "KG " "KG " "KG " "KG " ...
$ V26: chr "NO " "NO " "NO " "NO " ...
$ V27: chr "Pure-bred breeding horses "| __truncated__ "Pure-bred breeding asses "| __truncated__ "Pure-bred breeding horses "| __truncated__ "Horses for slaughter "| __truncated__ ...
至于编码问题,我无法提供进一步的见解:
Encoding (readLines("~/Downloads/SMKA12_2012archive/SMKA121212", n=1))
#[1] "unknown"
关于读入带有异常字符的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19301265/