我的字符串是这样的:
2 cups - Desiccated Coconut, 2.5 cups - Maida, 1.25 cup - Butter, 1/2 tsp - Baking soda, 1.25 cup - Powdered Sugar, 1/2 tsp - Baking powder, 2-3 drops - Vanilla essence
我希望输出格式如下:
答案 0 :(得分:0)
这是一个解决方案,您只需要最终为变量指定正确的名称。
下次,请证明你尝试了一些东西或调查了一下。您将需要包txt= c("2 cups - Desiccated Coconut, 2.5 cups - Maida, 1.25 cup - Butter, 1/2 tsp - Baking soda, 1.25 cup - Powdered Sugar, 1/2 tsp - Baking powder, 2-3 drops - Vanilla essence")
splits_per_ingredients = strsplit(txt,c(","))[[1]]
split_per_quantity = sapply(splits_per_ingredients,strsplit," - ")
split_per_dose=t(sapply(split_per_quantity,FUN=function(vec) {c(strsplit(stringr::str_trim(vec[1])," ")[[1]],vec[2])}))
来删除尾随和前导空格。
[,1] [,2] [,3]
2 cups - Desiccated Coconut "2" "cups" "Desiccated Coconut"
2.5 cups - Maida "2.5" "cups" "Maida"
1.25 cup - Butter "1.25" "cup" "Butter"
1/2 tsp - Baking soda "1/2" "tsp" "Baking soda"
1.25 cup - Powdered Sugar "1.25" "cup" "Powdered Sugar"
1/2 tsp - Baking powder "1/2" "tsp" "Baking powder"
2-3 drops - Vanilla essence "2-3" "drops" "Vanilla essence"
输出:
<script type="text/javascript">
var arr = [];
var names = [{name : 'George'}, {name : 'Ringo'}, {name : 'Paul'}, {name : 'John'}];
var surnames = [{surname : 'Harrison'}, {surname : 'Starr'}, {surname : 'McCartney'}, {surname : 'Lennon'}];
for (i = 0; i < names.length; i++) {
arr['firstname'] = names[i];
for (j = 0; j < surnames.length; j++) {
arr['firstname']['surname'] = surnames[j];
arr['firstname']['surname']['index'] = i;
console.log(arr);
}
}
</script>
答案 1 :(得分:0)
> library(dplyr)
> library(tidyr)
> library(stringr)
>
> str = '2 cups - Desiccated Coconut, 2.5 cups - Maida, 1.25 cup - Butter, 1/2 tsp - Baking soda, 1.25 cup - Powdered Sugar, 1/2 tsp - Baking powder, 2-3 drops - Vanilla essence'
>
> df = data.frame(Qty = unlist(strsplit(str, ',')))
> df
Qty
1 2 cups - Desiccated Coconut
2 2.5 cups - Maida
3 1.25 cup - Butter
4 1/2 tsp - Baking soda
5 1.25 cup - Powdered Sugar
6 1/2 tsp - Baking powder
7 2-3 drops - Vanilla essence
>
> df = df %>% separate(Qty, c('Unit', 'Ingredient'), ' - ')
> df
Unit Ingredient
1 2 cups Desiccated Coconut
2 2.5 cups Maida
3 1.25 cup Butter
4 1/2 tsp Baking soda
5 1.25 cup Powdered Sugar
6 1/2 tsp Baking powder
7 2-3 drops Vanilla essence
>
> # remove start & end white spaces (stringr library)
> # or else you can't separate correctly
> df$Unit = str_trim(df$Unit)
> df = df %>% separate(Unit, c('Qty', 'Unit'), ' ')
> df
Qty Unit Ingredient
1 2 cups Desiccated Coconut
2 2.5 cups Maida
3 1.25 cup Butter
4 1/2 tsp Baking soda
5 1.25 cup Powdered Sugar
6 1/2 tsp Baking powder
7 2-3 drops Vanilla essence