在字符串中嵌入nul导入Content-Type的原始数据:text / tab-separated-values;字符集= UTF-16LE

问题描述 投票:0回答:2

使用httr使用oath2.0从站点获取报告我无法将原始内容转换为R中的字符集。

 > req <-GET("https://www.blah.com/blah/v2/blah", config(token = token))

我的回答表明没有问题:

 Response [https://www.blah.com/blah/v2/blah]
 Date: 2018-09-21 15:55
 Status: 200
 Content-Type: text/tab-separated-values; charset=utf-16le
 Size: 21.1 MB
NA

当我尝试将原始数据转换为char时,我得到:

> rawToChar(req$content)
Error in rawToChar(req$content) : 
embedded nul in string:

通过content()检查内容时,我也会收到以下错误:

> content(req)
Error in guess_header_(datasource, tokenizer, locale) :
Incomplete multibyte sequence

有什么想法吗?我在网上找到了有限的资源......

r http oauth utf-16 httr
2个回答
0
投票

以供参考。对于原始结构,'00'表示NULL。解决方案是删除所有NULL值,然后转换为char。

 > dat <- req$content
 > up_dat <- dat[!dat=='00']
 > rawToChar(up_dat)

一旦转换,删除对整体数据结构没有影响。

在这种情况下,

  readr::read_tsv()

工作得很好。


0
投票

您也可以使用readBin()读取您的原​​始矢量。唯一的事情是你需要知道或猜测用于n的大小。但你可以通过计算NUL值来计算它们。

count_nul <- length(dat[dat == 00])
readBin(dat, n = count_nul)
© www.soinside.com 2019 - 2024. All rights reserved.