从 javascript 中的 CSV 文件读取数据时如何解决奇怪的输出?
How to resolve weird output while reading data from a CSV file in javascript?
我正在读取一个 csv 文件并想将其转换成数组字符串。我的文件如下所示:
https://www.google.com/imghp?hl=en&tab=wi
https://maps.google.com/maps?hl=en&tab=wl
https://play.google.com/?hl=en&tab=w8
https://www.youtube.com/?gl=US&tab=w1
https://news.google.com/nwshp?hl=en&tab=wn
https://mail.google.com/mail/?tab=wm
https://drive.google.com/?tab=wo
https://www.google.com/intl/en/about/products?tab=wh
https://accounts.google.com/ServiceLogin?hl=en&passive=true&continue=https://www.google.com/
https://www.google.com/url?q=https://lifeinaday.youtube/%3Futm_source%3Dgoogle%26utm_medium%3Dhppcta%26utm_campaign%3D2020&source=hpp&id=19019062&ct=3&usg=AFQjCNEJMAD58Mjdnro8Mjm-RtJ3nfEIZA&sa=X&ved=0ahUKEwi98PWM4-HqAhVh1uAKHeYGCPwQ8IcBCAU
我运行这段代码读取上面的csv文件并将数据推送到csv_data数组
const fs = require('fs');
const Papa = require('papaparse');
const csvFilePath = 'anchors.csv'
const file = fs.createReadStream(csvFilePath);
var csvData=[];
Papa.parse(file, {
header: true,
step: function(result) {
csvData.push(result.data)
},
complete: function(results, file) {
console.log('Complete', csvData.length, 'records.');
console.log(csvData)
}
});
但是,我得到这个用冒号和大括号分隔的奇怪输出。
[
{
'https://www.google.com/imghp?hl=en&tab=wi': 'https://maps.google.com/maps?hl=en&tab=wl'
},
{
'https://www.google.com/imghp?hl=en&tab=wi': 'https://play.google.com/?hl=en&tab=w8'
},
{
'https://www.google.com/imghp?hl=en&tab=wi': 'https://www.youtube.com/?gl=US&tab=w1'
},
{
'https://www.google.com/imghp?hl=en&tab=wi': 'https://news.google.com/nwshp?hl=en&tab=wn'
},
{
'https://www.google.com/imghp?hl=en&tab=wi': 'https://mail.google.com/mail/?tab=wm'
},
{
'https://www.google.com/imghp?hl=en&tab=wi': 'https://drive.google.com/?tab=wo'
},
{
'https://www.google.com/imghp?hl=en&tab=wi': 'https://www.google.com/intl/en/about/products?tab=wh'
},
{
'https://www.google.com/imghp?hl=en&tab=wi': 'https://accounts.google.com/ServiceLogin?hl=en&passive=true&continue=https://www.google.com/'
},
{
'https://www.google.com/imghp?hl=en&tab=wi': 'https://www.google.com/url?q=https://lifeinaday.youtube/%3Futm_source%3Dgoogle%26utm_medium%3Dhppcta%26utm_campaign%3D2020&source=hpp&id=19019062&ct=3&usg=AFQjCNEJMAD58Mjdnro8Mjm-RtJ3nfEIZA&sa=X&ved=0ahUKEwi98PWM4-HqAhVh1uAKHeYGCPwQ8IcBCAU'
}
]
我在这里做错了什么。我只需要用 comma.Any 分隔的整个字符串 help 就是 appreciated.Thanks.
这里没问题,在 CSV 文件中,第一行是 header-row(您的列的名称)。这里有一个名为 'https://www.google.com/imghp?hl=en&tab=wi' 的列,所有后续行都是 value-rows.
尝试通过 header: false.
我正在读取一个 csv 文件并想将其转换成数组字符串。我的文件如下所示:
https://www.google.com/imghp?hl=en&tab=wi
https://maps.google.com/maps?hl=en&tab=wl
https://play.google.com/?hl=en&tab=w8
https://www.youtube.com/?gl=US&tab=w1
https://news.google.com/nwshp?hl=en&tab=wn
https://mail.google.com/mail/?tab=wm
https://drive.google.com/?tab=wo
https://www.google.com/intl/en/about/products?tab=wh
https://accounts.google.com/ServiceLogin?hl=en&passive=true&continue=https://www.google.com/
https://www.google.com/url?q=https://lifeinaday.youtube/%3Futm_source%3Dgoogle%26utm_medium%3Dhppcta%26utm_campaign%3D2020&source=hpp&id=19019062&ct=3&usg=AFQjCNEJMAD58Mjdnro8Mjm-RtJ3nfEIZA&sa=X&ved=0ahUKEwi98PWM4-HqAhVh1uAKHeYGCPwQ8IcBCAU
我运行这段代码读取上面的csv文件并将数据推送到csv_data数组
const fs = require('fs');
const Papa = require('papaparse');
const csvFilePath = 'anchors.csv'
const file = fs.createReadStream(csvFilePath);
var csvData=[];
Papa.parse(file, {
header: true,
step: function(result) {
csvData.push(result.data)
},
complete: function(results, file) {
console.log('Complete', csvData.length, 'records.');
console.log(csvData)
}
});
但是,我得到这个用冒号和大括号分隔的奇怪输出。
[
{
'https://www.google.com/imghp?hl=en&tab=wi': 'https://maps.google.com/maps?hl=en&tab=wl'
},
{
'https://www.google.com/imghp?hl=en&tab=wi': 'https://play.google.com/?hl=en&tab=w8'
},
{
'https://www.google.com/imghp?hl=en&tab=wi': 'https://www.youtube.com/?gl=US&tab=w1'
},
{
'https://www.google.com/imghp?hl=en&tab=wi': 'https://news.google.com/nwshp?hl=en&tab=wn'
},
{
'https://www.google.com/imghp?hl=en&tab=wi': 'https://mail.google.com/mail/?tab=wm'
},
{
'https://www.google.com/imghp?hl=en&tab=wi': 'https://drive.google.com/?tab=wo'
},
{
'https://www.google.com/imghp?hl=en&tab=wi': 'https://www.google.com/intl/en/about/products?tab=wh'
},
{
'https://www.google.com/imghp?hl=en&tab=wi': 'https://accounts.google.com/ServiceLogin?hl=en&passive=true&continue=https://www.google.com/'
},
{
'https://www.google.com/imghp?hl=en&tab=wi': 'https://www.google.com/url?q=https://lifeinaday.youtube/%3Futm_source%3Dgoogle%26utm_medium%3Dhppcta%26utm_campaign%3D2020&source=hpp&id=19019062&ct=3&usg=AFQjCNEJMAD58Mjdnro8Mjm-RtJ3nfEIZA&sa=X&ved=0ahUKEwi98PWM4-HqAhVh1uAKHeYGCPwQ8IcBCAU'
}
]
我在这里做错了什么。我只需要用 comma.Any 分隔的整个字符串 help 就是 appreciated.Thanks.
这里没问题,在 CSV 文件中,第一行是 header-row(您的列的名称)。这里有一个名为 'https://www.google.com/imghp?hl=en&tab=wi' 的列,所有后续行都是 value-rows.
尝试通过 header: false.