我尝试使用csvtojson
const csv = require("csvtojson");
const csvFilePath = require('./../../books.csv');
csv()
.fromFile(csvFilePath)
.then((jsonObj)=>{
console.log(jsonObj);
})我的file.csv是
Book,Author,Amount,Price
The Compound Effect,Darren Hardy,5,9.48
The 7 Habits of Highly Effective People,Stephen R. Covey,4,23.48
The Miracle Morning,Hal Elrod,10,21.34
Influence: The Psychology of Persuasion,Robert B. Cialdini,4,12.99
The ONE Thing,Gary Keller,1,11.18我有下一个错误
The Compound Effect,Darren Hardy,5,9.48
^^^^^^^^
SyntaxError: Unexpected identifier
at Module._compile (internal/modules/cjs/loader.js:723:23)怎么啦?谢谢)
发布于 2019-11-20 07:30:20
这里的问题是,您正在使用函数require('./../../books.csv');导入文件。
在阅读完文档之后,我可以保证您只需要传递CSV文件的路径。
因此,您的代码应该如下所示:
const csv = require('csvtojson');
const csvFilePath = './../../books.csv';
csv().fromFile(csvFilePath).then((jsonObj)=>{
console.log(jsonObj);
})并且期望的输出应该是:
[ { Book: 'The Compound Effect',
Author: 'Darren Hardy',
Amount: '5',
Price: '9.48' },
{ Book: 'The 7 Habits of Highly Effective People',
Author: 'Stephen R. Covey',
Amount: '4',
Price: '23.48' },
{ Book: 'The Miracle Morning',
Author: 'Hal Elrod',
Amount: '10',
Price: '21.34' },
{ Book: 'Influence: The Psychology of Persuasion',
Author: 'Robert B. Cialdini',
Amount: '4',
Price: '12.99' },
{ Book: 'The ONE Thing',
Author: 'Gary Keller',
Amount: '1',
Price: '11.18' } ]发布于 2019-11-20 06:50:50
我认为应该在.csv文件中包含",或者使用quote作为处理选项
发布于 2019-11-20 16:04:09
已通过更改路径解决此问题)
const csvFilePath = './../../books.csv';至
const csvFilePath = `${__dirname}/../../books.csv`;然而,我不认为在这种情况下使用'__dirname‘是一个好主意。如果你对此有什么想法,请写下来)
https://stackoverflow.com/questions/58943937
复制相似问题