我有字符串列表库存(下方)
stock
[[1]]
[1] "[3,black,sku14422032,Out of stock,21.0,null,null,37RFM2I]"
[2] "[4,black,sku14421965,Out of stock,21.0,null,null,37RFM2I]"
[3] "[5,black,sku14421982,Out of stock,21.0,null,null,37RFM2I]"
[4] "[6,black,sku14421990,Out of stock,21.0,null,null,37RFM2I]"
[5] "[7,black,sku14422002,In stock,21.0,null,null,37RFM2I]"
[6] "[8,black,sku14422013,In stock,21.0,null,null,37RFM2I]"
我想转换成
colour = black
size = 3,4,5,6,7,8
stock = 3-out of stock, 4-out of stock, ...., 8 - In stock
我试过str_split_fixed但它只是拆分了第一行。
最初字符串采用这种格式
"stockMatrix = [\n[\"3\",\"black\",\"sku14422032\",\"Out of stock#Sorry, the option you have selected is out of stock\",\"21.0\",null,null,\"37RFM2I\",\"\"]\n,\n[\"4\",\"black\",\"sku14421965\",\"Out of stock#Sorry, the option you have selected is out of stock\",\"21.0\",null,null,\"37RFM2I\",\"\"]\n,\n[\"5\",\"black\",\"sku14421982\",\"Out of stock#Sorry, the option you have selected is out of stock\",\"21.0\",null,null,\"37RFM2I\",\"\"]\n,\n[\"6\",\"black\",\"sku14421990\",\"Out of stock#Sorry, the option you have selected is out of stock\",\"21.0\",null,null,\"37RFM2I\",\"\"]\n,\n[\"7\",\"black\",\"sku14422002\",\"In stock#\",\"21.0\",null,null,\"37RFM2I\",\"\"]\n,\n[\"8\",\"black\",\"sku14422013\",\"In stock#\",\"21.0\",null,null,\"37RFM2I\",\"\"]\n];"
我使用下面的代码来清理字符串
stock = regexpr("stockMatrix(.*?);",srcpage)
stock = regmatches (srcpage,stock)
stock= str_replace_all(stock,'stockMatrix = |;|\\n|"|#|#Sorry, the option you have selected is out of stock',"")
stock = str_replace_all(stock,"^\\[\\[","[")
stock = str_replace_all(stock,",\\]\\]|\\,]","]")
stock = str_extract_all(stock,"\\[(.*?)\\]")
...获取列表级别。这样做有更好的方法吗?
答案 0 :(得分:2)
您需要strsplit
内容stock[[1]]
,而不是stock
:
res.mx <- do.call(
rbind,
strsplit(substr(stock[[1]], 2, nchar(stock[[1]]) - 1), ",")
)
colnames(res.mx) <- c("size", "color", "sku#", "stockstatus", "price", "x", "y", "prod.code")
res.mx
这会产生:
size color sku# stockstatus price x y prod.code
[1,] "3" "black" "sku14422032" "Out of stock" "21.0" "null" "null" "37RFM2I"
[2,] "4" "black" "sku14421965" "Out of stock" "21.0" "null" "null" "37RFM2I"
[3,] "5" "black" "sku14421982" "Out of stock" "21.0" "null" "null" "37RFM2I"
[4,] "6" "black" "sku14421990" "Out of stock" "21.0" "null" "null" "37RFM2I"
[5,] "7" "black" "sku14422002" "In stock" "21.0" "null" "null" "37RFM2I"
[6,] "8" "black" "sku14422013" "In stock" "21.0" "null" "null" "37RFM2I"