我有一个带有Id key和Long desc的CSV文件。Id key只是一个字符串,但Long desc是HTML。
我的目标是将CSV文件解析为JSON。(请参见Output)
问题是,我不能在"上分割它,因为有些属性像color: ""red"",有些文本包括",例如Charger "15W"。我的另一个想法是拆分;,它位于Id key示例KE4I2-21;之后,但是也有一些包含;: 的;行。
我正在使用node.js,我尝试使用一些CSV到JSON包转换器,但它们没有成功地解析这些数据。
知道如何将这个奇怪的CSV文件转换成JSON吗?(我知道我的输出示例不正确,因为我正在打开和关闭")
我首先将所有""替换为',如下所示:.replace(/""/g, "'")
CSV文件(desc.csv)
Id key;Long desc
KE4I2-21;"<p color=""red""><strong>Charger "15W" - Black</strong></p>
<iframe src=""https://www.youtube.com/embed/XXXXXX"" width=""560"" height=""315"" frameborder=""0"" allowfullscreen=""allowfullscreen""></iframe>
<p><strong>More</strong>: </p>
<ul>
<li><strong>List</li>
<li><strong>Märke</strong>: SiGN</li>
</ul>"
LE0PP;"<p>Type-C charger<br /> - OnePlus 2<br /></p>
<p><em>Warning</em></p>"
T12-XRE2;"<h2> </h2>
<h2><strong>Car Charger</strong></h2>
<p>Lorem ipsum dolor...</p>
<p><strong>Assets:</strong></p>
<ul>
<li><strong>Something</strong>Nice</strong> Ja</li>
<li><strong>Other</strong>Things here</strong> Ja</li>
</ul>"输出
[
{
"Id key": "KE4I2-21",
"Long desc": "<p color="red"><strong>Charger "15W" - Black</strong></p><iframe src="https://www.youtube.com/embed/XXXXXX" width="560" height="315" frameborder="0" allowfullscreen="allowfullscreen"></iframe><strong>More</strong>: </p>\n\n<ul>\n\n<li><strong>List</li>\n\n<li><strong>Märke</strong>: SiGN</li>\n\n</ul>"
},
{
"Id key": "LE0PP",
"Long desc": "<p>Type-C charger<br /> - OnePlus 2<br /></p>\n\n<p><em>Warning</em></p>"
},
{
"Id key": "T12-XRE2",
"Long desc": "<h2> </h2>\n\n<h2><strong>Car Charger</strong></h2>\n\n<p>Lorem ipsum dolor...</p>\n\n<p><strong>Assets:</strong></p>\n\n<ul>\n\n<li><strong>Something</strong>Nice</strong> Ja</li>\n\n<li><strong>Other</strong>Things here</strong> Ja</li>\n\n</ul>"
}
]csvtojson包就是这样解析csv文件的。
const CSVToJSON = require("csvtojson");
(async () => {
let descriptions = await CSVToJSON().fromFile("./desc.csv");
console.log(descriptions)
})();
// Output
[
{
'Id key;Long desc': 'KE4I2-21;"<p color=""red""><strong>Charger "15W" - Black</strong></p>'
},
{
'Id key;Long desc': '<iframe src=""https://www.youtube.com/embed/XXXXXX"" width=""560"" height=""315"" frameborder=""0"" allowfullscreen=""allowfullscreen""></iframe>'
},
{ 'Id key;Long desc': '<p><strong>More</strong>: </p>' },
{ 'Id key;Long desc': '<ul>' },
{ 'Id key;Long desc': '<li><strong>List</li>' },
{ 'Id key;Long desc': '<li><strong>Märke</strong>: SiGN</li>' },
{ 'Id key;Long desc': '</ul>"' },
{
'Id key;Long desc': 'LE0PP;"<p>Type-C charger<br /> - OnePlus 2<br /></p>'
},
{ 'Id key;Long desc': '<p><em>Warning</em></p>"' },
{ 'Id key;Long desc': 'T12-XRE2;"<h2> </h2>' },
{ 'Id key;Long desc': '<h2><strong>Car Charger</strong></h2>' },
{ 'Id key;Long desc': '<p>Lorem ipsum dolor...</p>' },
{ 'Id key;Long desc': '<p><strong>Assets:</strong></p>' },
{ 'Id key;Long desc': '<ul>' },
{
'Id key;Long desc': '<li><strong>Something</strong>Nice</strong> Ja</li>'
},
{
'Id key;Long desc': '<li><strong>Other</strong>Things here</strong> Ja</li>'
},
{ 'Id key;Long desc': '</ul>"' }
]发布于 2020-11-30 18:24:24
因此,我找到了一种将CSV文件解析为有效的JSON对象数组的“脏”方法。
let desc = fs.readFileSync("./files/TD-products-beskrivning html.csv", "utf-8");
desc = desc
.replace(/Id key;Long desc/g, "")
.replace(/\r/g, " ")
.replace(/\n/g, " ")
.replace(/""/g, "'")
.replace(/ /g, " ");
desc = desc.split(';"');
let ids = [];
desc.forEach(element => {
let n = element.split(" ");
ids.push(n[n.length - 1]);
});
// Remove empty id & desc
ids.pop();
desc.shift();
let descriptions = []
for (let i = 0; i < ids.length; i++) {
let descriptionObject = {};
descriptionObject["Id key"] = ids[i];
descriptionObject["Long desc"] = desc[i];
descriptions.push(descriptionObject);
}
console.log(descriptions);输出
[
{
"Id key": "KE4I2-21",
"Long desc": "<p color='red'><strong>Charger '15W' - Black</strong></p><iframe src='https://www.youtube.com/embed/XXXXXX' width='560' height='315' frameborder='0' allowfullscreen='allowfullscreen'></iframe><strong>More</strong>: </p>\n\n<ul>\n\n<li><strong>List</li>\n\n<li><strong>Märke</strong>: SiGN</li>\n\n</ul>"
},
{
"Id key": "LE0PP",
"Long desc": "<p>Type-C charger<br /> - OnePlus 2<br /></p>\n\n<p><em>Warning</em></p>"
},
{
"Id key": "T12-XRE2",
"Long desc": "<h2> </h2>\n\n<h2><strong>Car Charger</strong></h2>\n\n<p>Lorem ipsum dolor...</p>\n\n<p><strong>Assets:</strong></p>\n\n<ul>\n\n<li><strong>Something</strong>Nice</strong> Ja</li>\n\n<li><strong>Other</strong>Things here</strong> Ja</li>\n\n</ul>"
}
]https://stackoverflow.com/questions/65072508
复制相似问题