我需要阅读一些包含许多行的json文件。
在某些文件中,我看到一个空行,当我尝试将文件读入R(使用jsonlite)时会导致错误。
如何使用R?
删除任何空行以下是带空行的部分示例:
mouse","trial_index":92,"time_elapsed":255348,"internal_node_id":"0.0-3.0-2.0-27.0","subject":"693","answer":1},{"rt":-1,"stimulus":"/study_assets/creactive/static/images/bird.jpeg","key_press":-1,"block":"two-back","condition":"target","trial_type":"single-stim-mouse","trial_index":93,"time_elapsed":257160,"internal_node_id":"0.0-3.0-2.0-28.0","subject":"693","answer":0},{"rt":407,"stimulus":"/study_assets/creactive/static/images/bird.jpeg","key_press":"mouse","block":"two-back","condition":"distractor","trial_type":"single-stim-mouse","trial_index":94,"time_elapsed":257871,"internal_node_id":"0.0-3.0-2.0-29.0","subject":"693","answer":0}]
[{"rt":71,"key_press":"mouse","trial_type":"text","trial_index":0,"time_elapsed":74,"internal_node_id":"0.0-0.0","subject":"695"},{"rt":-1,"correct":false,"stimulus":"<DIV id=\"goNoGoFixationPoint\">+</DIV>","key_press":-1,"response":"fixation","trial_type":"categorize-mouse","trial_index":1,"time_elapsed":575,"internal_node_id":"0.0-1.0-0.0-0.0","subject":"695"}
答案 0 :(得分:0)
所以最后我使用readLines和一个带nchar()的循环,如下所示:
x = JSONFILE
readLines(x, skipNul= TRUE)
for (i in 1:length(x)) {
if(nchar(x[i]==0) {
x <- x[-i]
}
}