简体   繁体   English

如何从 csv 文件中提取特定对象?

[英]How to pull a specific object from a csv file?

I am trying to get a specific part of this csv file but I cant get it to work correctly.我正在尝试获取此 csv 文件的特定部分,但无法使其正常工作。 when I try to pull a piece of it I get the entire column.当我试图拉出它的一部分时,我得到了整个列。 Here is what the file looks like and my code:这是文件的外观和我的代码:

Demand.csv需求.csv

Time,Day ahead forecast,Hour ahead forecast,Current demand
00:00,23307,22058,21942
00:05,21744,21822,21849
00:10,21744,21822,21908
00:15,21744,21822,21809
00:20,21744,21615,21736
00:25,21744,21615,21688
00:30,21744,21615,21563
00:35,21744,21371,21479
00:40,21744,21371,21378
00:45,21744,21371,21256

code代码

const fs=require('fs')
const csv = require('csv-parser');


fs.createReadStream('demand.csv')
  .pipe(csv())
  .on('data', function(data){
    try {
        console.log(data.Time[1]);  
    }
    catch(err) {
        //error handler
        console.log('error');
    }
     })
  .on('end', function() {
    //some final operation
}); 

The Output is:输出是:

0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
1
1
1
1
1
1
1
2
2
2
2

and so on till the it reaches the end of the CSV.依此类推,直到它到达 CSV 的末尾。

I have tried a couple different ways and nothing seems to have worked.我尝试了几种不同的方法,但似乎没有任何效果。 I am very new to Javascript so any help is appreciated.我对 Javascript 很陌生,因此感谢您的任何帮助。 an example of what I need is something that can pull row 3 column 1. 00:10 being the output.我需要的一个例子是可以拉出第 3 行第 1 列的东西。00:10 是输出。 Thank you.谢谢你。

The problem is the 'CSV' file: your screenshot shows you had to enable multiple separator options (commas AND tabs) to use your import tool.问题在于“CSV”文件:您的屏幕截图显示您必须启用多个分隔符选项(逗号和制表符)才能使用您的导入工具。

Try sticking to one separator: comma尝试使用一个分隔符:逗号

Time,Day ahead forecast,Hour ahead forecast,Current demand
00:00,23307,22058,21942
00:05,21744,21822,21849
00:10,21744,21822,21908
00:15,21744,21822,21809
00:20,21744,21615,21736
00:25,21744,21615,21688
00:30,21744,21615,21563
00:35,21744,21371,21479
00:40,21744,21371,21378
00:45,21744,21371,21256

It appears csv-parser parses the csv into an array of objects.看来csv-parser将 csv 解析为一个对象数组。

To access row 3 column 1 it would be要访问第 3 行第 1 列,它将是

console.log(data[3].Time);

Data is an array with each element corresponding to a 'row' of data.数据是一个array ,每个元素对应一个“行”数据。 So to access row 3 it would be data[3] etc.因此,要访问第 3 行,它将是data[3]等。

Each individual 'column' is accessed by the object key or the column header from each column in the csv .每个单独的“列”都由csv每一列的对象key或列标题访问。

To access object keys with spaces you can use "bracket notation"要使用空格访问对象键,您可以使用“括号表示法”

console.log(data[3].['Day ahead forecast']);

So the issue could be due to no constancy Time Day ahead forecast, Hour ahead forecast, Current demand.因此,问题可能是由于没有固定Time Day ahead forecast, Hour ahead forecast, Current demand. You can fix this just like @searlea suggested with comma, tab, or evn with pipe separated.您可以像@searlea 建议的那样使用comma, tab, or evn with pipe来解决此问题comma, tab, or evn with pipe分隔。

Here's a working example.这是一个工作示例。

https://repl.it/@subhendukundu/PaleWelcomeChord https://repl.it/@subhendukundu/PaleWe​​lcomeChord

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

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