R中的日志清理

R中的日志清理,r,text-analysis,R,Text Analysis,这是我在R中加载的日志模板的结构。如何清理它以生成数据帧 {"ask":{"Id":001,"TS":10012001,"Response":"12"}} {"ask":{"Id":002,"TS":11012001,"Response":"10"}} 预期输出应该是数据框中的各个列及其值,以便进一步分析。这里有一个快速解决方案: 1.将整个文件作为字符数组读取: sfile <- readLines(file) 3.现在将其读取为逗号分隔值(read.csv接受字符串而不是文件作为输

这是我在R中加载的日志模板的结构。如何清理它以生成数据帧

{"ask":{"Id":001,"TS":10012001,"Response":"12"}}
{"ask":{"Id":002,"TS":11012001,"Response":"10"}}

预期输出应该是数据框中的各个列及其值,以便进一步分析。

这里有一个快速解决方案:

1.将整个文件作为字符数组读取:

sfile <- readLines(file)
3.现在将其读取为逗号分隔值(
read.csv
接受字符串而不是文件作为输入)


下面是一个快速解决方案:

1.将整个文件作为字符数组读取:

sfile <- readLines(file)
3.现在将其读取为逗号分隔值(
read.csv
接受字符串而不是文件作为输入)


因为除了引号中包含的前导零编号字符串外,行几乎是有效的JSON,请考虑对有效JSON进行清洗,并将其导入JSOLITE中作为一行数据框。然后行绑定列表中所有单独的df元素。下面迭代读取日志中的行以转换每行:

library(jsonlite)

loglines <- readLines("/path/to/log.txt")

dfList <- lapply(loglines, function(line){
  # JSON CONVERT WITH QUOTE AND BRACKET WRAPPING
  jsonline <- paste0("[", gsub(',"TS', '","TS', gsub('Id":', 'Id":"', line)), "]")

  fromJSON(jsonline)[[1]]
})

df <- do.call(rbind, dfList)
rownames(df) <- NULL
library(jsonlite)

日志行因为行几乎是有效的JSON,除了应该用引号括起来的前导零编号字符串外,请考虑对JSON进行清洗,并将其导入JSOLITE作为一行数据框。然后行绑定列表中所有单独的df元素。下面迭代读取日志中的行以转换每行:

library(jsonlite)

loglines <- readLines("/path/to/log.txt")

dfList <- lapply(loglines, function(line){
  # JSON CONVERT WITH QUOTE AND BRACKET WRAPPING
  jsonline <- paste0("[", gsub(',"TS', '","TS', gsub('Id":', 'Id":"', line)), "]")

  fromJSON(jsonline)[[1]]
})

df <- do.call(rbind, dfList)
rownames(df) <- NULL
library(jsonlite)
日志行<代码>库(V8)
图书馆(jqr)
图书馆(tidyverse)
txt%
映射(jsonlite::fromJSON)%>%
地图(未列出)%>%
地图(如列表所示)
###A tible:2×3
##询问,询问,询问,回答
##                
## 1      1 10012001           12
## 2      2 11012001           10
jqr+ndjson助手
map(行,jq,“.”%>%
映射(标志,pretty=FALSE)%>%
map_df(~ndjson::flatte(.$data))
##资料来源:本地数据表[2 x 3]
## 
###tbl_dt[2×3]
##问。我问。回答问
##                
## 1      1           12 10012001
## 2      2           10 11012001
mutate()
图书馆(jqr)
图书馆(tidyverse)
txt%
映射(jsonlite::fromJSON)%>%
地图(未列出)%>%
地图(如列表所示)
###A tible:2×3
##询问,询问,询问,回答
##                
## 1      1 10012001           12
## 2      2 11012001           10
jqr+ndjson助手
map(行,jq,“.”%>%
映射(标志,pretty=FALSE)%>%
map_df(~ndjson::flatte(.$data))
##资料来源:本地数据表[2 x 3]
## 
###tbl_dt[2×3]
##问。我问。回答问
##                
## 1      1           12 10012001
## 2      2           10 11012001

mutate()?(我是移动的,所以根据字符串的结构建议)也许是
rjson::fromJSON
?(我是移动的,所以建议根据你的字符串结构)
s <- c( '{ "ask": { "Id":001, "TS":10012001, "Response":"12" }}',
        '{ "ask": { "Id":002, "TS":11012001, "Response":"10" }}'
      )
> gsub("{ \"ask\": { \"Id\":| \"TS\":| \"Response\":\"|\" }}", "", s, perl = TRUE)
[1] "001,10012001,12" "002,11012001,10"
library(jsonlite)

loglines <- readLines("/path/to/log.txt")

dfList <- lapply(loglines, function(line){
  # JSON CONVERT WITH QUOTE AND BRACKET WRAPPING
  jsonline <- paste0("[", gsub(',"TS', '","TS', gsub('Id":', 'Id":"', line)), "]")

  fromJSON(jsonline)[[1]]
})

df <- do.call(rbind, dfList)
rownames(df) <- NULL
library(V8)
library(jqr)
library(tidyverse)

txt <- '{"ask":{"Id":001,"TS":10012001,"Response":"12"}}
{"ask":{"Id":002,"TS":11012001,"Response":"10"}}'

lines <- readLines(textConnection(txt))
ctx <- v8()
map_df(lines, function(x) {
  ctx$eval(sprintf("var dat=%s", JS(x)))
  ctx$get("dat") %>%
    unlist() %>%
    as.list()
})
## # A tibble: 2 × 3
##   ask.Id   ask.TS ask.Response
##    <chr>    <chr>        <chr>
## 1      1 10012001           12
## 2      2 11012001           10
map(lines, jq, ".") %>%
  map(jsonlite::fromJSON) %>%
  map(unlist) %>%
  map_df(as.list)
## # A tibble: 2 × 3
##   ask.Id   ask.TS ask.Response
##    <chr>    <chr>        <chr>
## 1      1 10012001           12
## 2      2 11012001           10
map(lines, jq, ".") %>%
  map(flags, pretty=FALSE) %>%
  map_df(~ndjson::flatten(.$data))
## Source: local data table [2 x 3]
## 
## # tbl_dt [2 × 3]
##   ask.Id ask.Response   ask.TS
##    <dbl>        <chr>    <dbl>
## 1      1           12 10012001
## 2      2           10 11012001