我想编写trycatch
代码来处理从网上下载时的错误.
url <- c(
"http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html",
"http://en.wikipedia.org/wiki/Xz")
y <- mapply(readLines, con=url)
Run Code Online (Sandbox Code Playgroud)
这两个语句成功运行.下面,我创建一个不存在的Web地址:
url <- c("xxxxx", "http://en.wikipedia.org/wiki/Xz")
Run Code Online (Sandbox Code Playgroud)
url[1]
不存在.如何编写trycatch
循环(函数)以便:
我有以下代码:
urls <- c(
"xxxxx",
"http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html",
"http://en.wikipedia.org/wiki/Xz"
)
readUrl <- function(url) {
out <- tryCatch(
readLines(con=url, warn=FALSE),
error=function(e) {
message(paste("URL does not seem to exist:", url))
message(e)
return(NA)
},
finally=message(paste("Processed URL:", url))
)
return(out)
}
y <- lapply(urls, readUrl)
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到:
URL does not seem to exist: xxxxx
cannot open the connectionProcessed URL: xxxxx
Processed URL: http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html
Processed URL: http://en.wikipedia.org/wiki/Xz
Warning message:
In file(con, "r") : cannot open file 'xxxxx': No such file or directory
Run Code Online (Sandbox Code Playgroud)
但我期待:
URL does not seem to …
Run Code Online (Sandbox Code Playgroud) 我you
以三种不同的形式编辑三个文件,其中包含相同的内容"你"(英文) - gbk\utf-8\ucs-2,gedit名为"ok1,ok2,ok3".
>>> f1 = open('ok1', 'rb').read()
>>> f2 = open('ok2', 'rb').read()
>>> f3 = open('ok3', 'rb').read()
>>> f1
'\xc4\xe3\n'
>>> f2
'\xe4\xbd\xa0\n'
>>> f3
'`O\n\x00'
>>> hex(ord("`"))
'0x60'
>>> hex(ord("O"))
'0x4f'
Run Code Online (Sandbox Code Playgroud)
事实上f3是'\ x60\x4f',但以下输出让我很困惑
>>> '\xe4\xbd\xa0'.decode("utf-8")
u'\u4f60'
>>> '\xc4\xe3'.decode("gbk")
u'\u4f60'
>>>
Run Code Online (Sandbox Code Playgroud)
为什么只有在ucs-2(或说unicode)中存在endian问题,而不是在utf-8中,而不是在gbk中?
我很好奇:
> strsplit("ty,rr", split = ",")
[[1]]
[1] "ty" "rr"
> strsplit("ty|rr", split = "|")
[[1]]
[1] "t" "y" "|" "r" "r"
Run Code Online (Sandbox Code Playgroud)
我为什么不c("ty","rr")
来自strsplit("ty|rr", split="|")
?
我可以使用do.call函数将list转换为data.frame:
z=list(c(1:3),c(5:7),c(7:9))
x=as.data.frame(do.call(rbind,z))
names(x)=c("one","two","three")
x
## one two three
## 1 1 2 3
## 2 5 6 7
## 3 7 8 9
Run Code Online (Sandbox Code Playgroud)
我想让它更简洁,将两个陈述合并为一个陈述,我可以吗?
x=as.data.frame(do.call(rbind,z))
names(x)=c("one","two","three")
Run Code Online (Sandbox Code Playgroud) 当我在python的struct模块中将数字更改为十六进制时,
>>> import struct
>>> struct.pack("i",89)
'Y\x00\x00\x00'
>>> struct.pack("i",890)
'z\x03\x00\x00'
>>> struct.pack("i",1890)
'b\x07\x00\x00'
Run Code Online (Sandbox Code Playgroud)
输出中"Y,z,b"的含义是什么?
这是一个字符串:
str1="ha,hihi,aaaaa,ok"
Run Code Online (Sandbox Code Playgroud)
我想要得到的位置","
中str1
,这可以算3,8,14
.
我怎么能在R中得到它?
我学习了8个月的python,新手到R,有一个二进制文件,我可以读取
并将二进制数据更改为一个列表(在python中,数组是列表).
数据文件(名为test)位于:
https://www.box.com/s/0g3qg2lqgmr7y7fk5aut
结构是:
每4个字节是一个整数,所以要在python中使用unpack读取它
import struct
datafile=open('test','rb')
data=datafile.read(32)
result=[]
while data:
result.append(list(struct.unpack('iiiiiiii',data)))
data=datafile.read(32)
Run Code Online (Sandbox Code Playgroud)
我如何读取R中的二进制数据?
datafile="test"
totalsize=file.info(datafile)$size
lines=totalsize/32
data=readBin("test",integer(),n=totalsize,size=4,endian="little")
result=data.frame(matrix(data,nrow=lines,ncol=8,byrow=TRUE))
colnames(result)=c(date,"x1","x2","x3","x4","x5","x6","x7")
Run Code Online (Sandbox Code Playgroud)
还有我想解决的问题.在这里,我用n = totalsize完全读取所有数据,如果数据很大,内存不足以包含,如何表达:从第1001到第2000字节读取数据?如果n = 1000,则表示从第1到第1000读取数据,如果n = 2000,则表示从第1到第2000读取数据,如何从第1001到第2000读取数据?R中有文件指针吗?当我读取第1000个二进制数据时,文件指针位于第1000个位置,现在使用命令readBin("test",integer(),n = 1000,size = 4,endian ="little" )从第1001到第2000读取数据?
在python中:
>>> "\xc4\xe3".decode("gbk").encode("utf-8")
'\xe4\xbd\xa0'
>>> "\xc4\xe3".decode("gbk")
u'\u4f60'
Run Code Online (Sandbox Code Playgroud)
我们可以得出两个结论:
1.\xc4\xe3 in gbk encode =\xe4\xbd\xa0 in utf-8
2.\xc4\xe3 in gbk encode =\x4f\x60 in unicode(或者说ucs-2)
在R:
> iconv("\xc4\xe3",from="gbk",to="utf-8",toRaw=TRUE)
[[1]]
[1] e4 bd a0
> iconv("\xc4\xe3",from="gbk",to="unicode",toRaw=TRUE)
[[1]]
[1] ff fe 60 4f
Run Code Online (Sandbox Code Playgroud)
现在,结论1是正确的,它在python中与在R中的
结论相同2是一个谜题,
究竟是什么在gbk编码中的\ xc4\xe3 = ?? 在unicode.
在python中它是你'\ u4f60',在R中它是ff fe 60 4f
是相等的吗?哪一个是正确的?它们都是正确的吗?