简体   繁体   English

如何删除节点中数组周围的引号?

[英]how to remove quotation marks around array in node?

I processed a data in python and saved the file in JSON format.我处理了 python 中的数据并将文件保存为 JSON 格式。

However, I ran into a problem after getting the JSON file when I use them in node.js which is但是,当我在 node.js 中使用 JSON 文件后遇到问题

the JSON file looks like as below. JSON 文件如下所示。

{
    "title": "Christmas Eggnog",
    "ingredients": "['whites', 'yolks', 'sugar', 'rye', 'whiskey', 'brandy', 'rum', 'cream']",
    "id": "05zEpbSqcs9E0rcnCJWyZ9OgdH0MLby"
},

The ingredients has to be a normal array however, it is an array held by quotation marks.成分必须是一个普通数组,但是它是一个用引号括起来的数组。 And it eventually makes it impossible to read this json file as intended.最终导致无法按预期读取此 json 文件。

{
    "title": "Christmas Eggnog",
    "ingredients": ["whites", "yolks", "sugar", "rye", "whiskey", "brandy", "rum", "cream"],
    "id": "05zEpbSqcs9E0rcnCJWyZ9OgdH0MLby"
},

I would like to get the log value like below.我想得到如下的日志值。 When I read this JSON file in node.当我在节点中读取这个 JSON 文件时。

const fs = require('fs');

let predata = fs.readFileSync('./data/testonce.json')
let data = JSON.parse(predata)
let newData = [];
data.forEach((d) => {
    newData.push((d) => {
        let targetarray = d.ingredients.split(',')
        return {...d, ingredients: targetarray }
    })
})

console.log(newData[0])
//"whites"

I can't get the string "white" because node doesn't understand the fact that "['whites', 'yolks', 'sugar', 'rye', 'whiskey', 'brandy', 'rum', 'cream']" is an array.我无法得到字符串“white”,因为节点不理解"['whites', 'yolks', 'sugar', 'rye', 'whiskey', 'brandy', 'rum', 'cream']"是一个数组。 Node sees it as one long string.节点将其视为一个长字符串。

How do I turn this string into a normal array?如何将此字符串转换为普通数组?

Edit)编辑)

My python code is as below.我的 python 代码如下。

import pandas as pd

df = pd.read_csv('./test.csv')

ingredients = df['ingredients']

for i in range(len(ingredients)):
    ingredients[i]=str(ingredients[i])[1:-1]

ingredients

df['ingredients']=ingredients

df.to_csv ('./datatest.csv', index = False, header=True)

After this, I used csv->json formatter in this website.之后,我在这个网站上使用了 csv->json 格式化程序。 https://csvjson.com/[![enter image description here] 1 ] 1 https://csvjson.com/[![在此处输入图像描述] 1 ] 1

Edit)编辑)

this is the csv file link这是 csv 文件链接

we.tl/t-Kw435qpNNd we.tl/t-Kw435qpNNd

It looks like this看起来像这样在此处输入图像描述

Edit) Sample csv text This is the sample csv text.编辑)示例 csv 文本 这是示例 csv 文本。

title,ingredients,id Christmas Eggnog,"['whites', 'yolks', 'sugar', 'rye', 'whiskey', 'brandy', 'rum', 'cream']",05zEpbSqcs9E0rcnCJWyZ9OgdH0MLby "Veal, Carrot and Chestnut Ragoût ","['chestnuts', 'veal', 'stew', 'meat', 'oil', 'onion', 'garlic', 'leaf', 'salt', 'chicken broth', 'wine', 'carrots', 'sage']",mF5SZmoqxF4WtIlhLRvzuKk.z6s7P2S Caramelized Bread Pudding with Chocolate and Cinnamon,"['butter', 'brioche', 'quality', 'bread', 'crusts', 'eggs', 'egg', 'yolks', 'brown', 'sugar', 'cream', 'milk', 'vanilla', 'cinnamon', 'nutmeg', 'kosher salt', 'chocolate', 'top']",oQV5D7cVbCFwmrDs3pBUv2y.AG0WV26 Sherried Stilton and Green Peppercorn Spread,"['stilton', 'cream cheese', 'peppercorns', 'brine', 'sherry']",Z9seBJWaB5NkSp4DQHDnCAUBTwov/1u Almond-Chocolate Macaroons,"['almonds', 'sugar', 'cinnamon', 'salt', 'egg', 'almond', 'semisweet', 'chocolate']",bB3GxoAplVZeoX3fzWNWyeECtQFxw6G White Sauce or Bechamel Sauce,"['butter', 'flour', 'milk', 'salt', 'pepper']",FHQAJvovVtPyKWlzgFEHgSUJsCM2Tj标题,成分,id 圣诞蛋酒,“['whites', 'yolks', 'sugar', 'rye', 'whiskey', 'brandy', 'rum', 'cream']",05zEpbSqcs9E0rcnCJWyZ9OgdH0MLby "Veal, Carrot and Chestnut Ragoût ","['栗子','小牛肉','炖','肉','油','洋葱','大蒜','叶子','盐','鸡汤','酒' , '胡萝卜', '鼠尾草']",mF5SZmoqxF4WtIlhLRvzuKk.z6s7P2S 巧克力肉桂焦糖面包布丁,"['黄油', '奶油蛋卷', '质量', '面包', '面包皮', '鸡蛋', '鸡蛋'、'蛋黄'、'棕色'、'糖'、'奶油'、'牛奶'、'香草'、'肉桂'、'肉豆蔻'、'犹太盐'、'巧克力'、'顶级']",oQV5D7cVbCFwmrDs3pBUv2y .AG0WV26 雪莉斯蒂尔顿和绿胡椒酱,“['stilton', 'cream cheese', 'peppercorns', 'brine', 'sherry']",Z9seBJWaB5NkSp4DQHDnCAUBTwov/1u Almond-Chocolate Macaroons,"['almonds', 'sugar ', '肉桂', '盐', '鸡蛋', '杏仁', '半甜', '巧克力']",bB3GxoAplVZeoX3fzWNWyeECtQFxw6G 白酱或调味酱,"['黄油', '面粉', '牛奶', '盐','胡椒']",FHQAJvovVtPyKWlzgFEHgSUJsCM2Tj q q

because its a string, you need to strip of the bad bits you do not want, and then convert what is left of the string into an array因为它是一个字符串,你需要去掉你不想要的坏位,然后将字符串的剩余部分转换成一个数组

var list = "['whites', 'yolks', 'sugar', 'rye', 'whiskey', 'brandy', 'rum', 'cream']".replace(/[\[\]["',]/g,'').split(" ");

The replace uses regex to remove the " ', [ ] characters, and the split cuts up the string by the remaining spaces and converts it into an array.替换使用正则表达式删除 " ', [ ] 字符,拆分将字符串通过剩余的空格分割并将其转换为数组。

console.log(list);
0: "whites"
1: "yolks"
2: "sugar"
3: "rye"
4: "whiskey"
5: "brandy"
6: "rum"
7: "cream"
length: 8
__proto__: Array(0)

I agree with Thierry Lathuille to fix the problem in the producer.我同意 Thierry Lathuille 解决制片人的问题。 If you you want to patch it up here, I would parse the string as JSON and just replace it:如果你想在这里修补它,我会将字符串解析为 JSON 并替换它:

data.forEach(r => {
    r.ingredients = JSON.parse(r.ingredients);
}

 const json = { "title": "Christmas Eggnog", "ingredients": "['whites', 'yolks', 'sugar', 'rye', 'whiskey', 'brandy', 'rum', 'cream']", "id": "05zEpbSqcs9E0rcnCJWyZ9OgdH0MLby" } const ingredients = json.ingredients.substr(1, json.ingredients.length -2) const ingredientsList = ingredients.split(', ').map(item => item.substr(1, item.length -2)) console.log(ingredientsList)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM