【发布时间】:2019-07-10 10:32:04
【问题描述】:
我正在尝试将以下数据转换为 csv 格式,其标题等于 json 中的键。
data= {
title: 'sample title',
problem: 'this sentence; is a problem', //<----
name: 'Anitta',
albumArt:'sample.png',
}
我使用csv-writer 将 json 转换为 csv。
我的问题是,当生成 csv 时,带有分号的字符串被拆分并插入到 2 个单独的单元格中。相反,我需要它在problem 列下。
预期结果
title | problem | name | albumArt
-------------|-----------------------------|--------|------------
sample title | this sentence; is a problem | Anitta | sample.png
当前结果
title | problem | name | albumArt |
-------------|----------------|--------------|----------|----------
sample title | this sentence | is a problem | Anitta | sample.png
有什么办法可以解决这个问题吗?
编辑
for (var key in data)
header.push({ id: key, title: key });
const csvWriter = createCsvWriter({
path: `./output/${folderName}/${folderName}.csv`,
header: header
});
csvWriter.writeRecords(data) // returns a promise
.then(() => {
console.log('Csv file created at :' +
`./output/${folderName}/${folderName}.csv`);
resolve();
});
【问题讨论】:
-
将列括在引号中,即
"sample title";"this sentence; is a problem"; "Anitta"; "sample.png" -
documentation 表明包含空格或分隔符的字符串已经用引号括起来。你能显示你正在使用的确切代码吗?
-
当您说“带分号的字符串被拆分并插入到 2 个单独的单元格中”时,您指的是 excel 吗?
-
请提供实际的 CSV 输出,当您使用它时 :)
-
“预期结果”——我希望它生成 CSV,而不是任何类型的表格布局。我怀疑您的真正问题是您用于解析 CSV 的软件中的错误/错误配置。
标签: javascript json csv