[英]Import a MySQL dump file into R
I have a line that look like this:我有一条看起来像这样的线:
INSERT INTO `table_name` VALUES (1,'some','body','+'), (2,'once','told me','+'), (3,'the world','is gonna roll me','+'))
And I'm trying to parse it as a dataframe such as:我正在尝试将其解析为 dataframe 例如:
tibble::tribble(
~col1, ~col2, ~col3, ~col4,
1, "some", "body", "+",
2, "once", "told me", "+",
3, "the world", "is gonna roll me", "+",
)
#> # A tibble: 3 × 4
#> col1 col2 col3 col4
#> <dbl> <chr> <chr> <chr>
#> 1 1 some body +
#> 2 2 once told me +
#> 3 3 the world is gonna roll me +
In short, take the content between each parentheses set as a line and separate it by ,
.简而言之,将每个括号之间的内容设置为一行,并用,
分隔。
Here's a quick and simple version - assumes that all lines always have 3 commas and that there aren't commas in the text itself but should get you started:这是一个快速简单的版本 - 假设所有行总是有 3 个逗号,并且文本本身没有逗号,但应该让你开始:
library(stringr)
library(tidyr)
v <- "INSERT INTO `table_name` VALUES (1,'some','body','+'), (2,'once','told me','+'), (3,'the world','is gonna roll me','+'))"
w <- data.frame(val=str_extract_all(v, "(?<=\\().*?(?=\\))")[[1]])
separate(w, val, into = paste0("col", 1:4), sep = ",")
which produces产生
col1 col2 col3 col4
1 1 'some' 'body' '+'
2 2 'once' 'told me' '+'
3 3 'the world' 'is gonna roll me' '+'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.